Unit Testing

1,854 views
1,759 views

Published on

Unit Tests, what are they? Styles and good practices.

Published in: Business, Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,854
On SlideShare
0
From Embeds
0
Number of Embeds
71
Actions
Shares
0
Downloads
31
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Unit Testing

  1. 1. Unit testing Vocabulary, styles, toys ____________________________
  2. 2. ____________________________ Vocabulary <ul><li>[…] I'm a pretty lazy person and am </li></ul><ul><li>prepared to work quite hard in order to avoid work. (Martin Fowler – The Value of Self Testing code) </li></ul><ul><li>self validating (tests check their own results) </li></ul><ul><li>fast </li></ul><ul><li>easy (no long tutorials before being able to use) </li></ul><ul><li>integrated </li></ul><ul><li>extendable </li></ul>J U nit framework JUnit framework has become Widely Used
  3. 3. ____________________________ Vocabulary “ Unit tests are tests written with JUnit” (naive, simplistic, false ) Unit Tests Integration, Functional, Acceptance Tests -> also written with JUnit If Unit tests cannot be defined via it’s Unit Testing Framework, what are they? Unit Testing : - tests that perform a verification of a unit of code (article) - “a unit test has no dependencies” (article) - a method of testing that verifies individual units of source code are working properly (wikipedia) Integration testing : - the part of software testing in which individual software modules are combined and tested together as a group.
  4. 4. ____________________________ Vocabulary Unit Tests & not just tests “ One of the challenges of unit testing is to sufficiently decouple the multiple units of code that make up an application so that each unit can be tested individually.” Unit Tests -> tend to become mini-integration tests! Why should I Care? Testing simple functionality (ex: new methods) in Integration Tests can lead to: Fragile Tests Unrepeatable Tests … Maintenance Nightmare 
  5. 5. ____________________________ Styles <ul><li>Non trivial tests: </li></ul><ul><li>Large number of dependencies </li></ul><ul><li>Awkward collaborations </li></ul><ul><li>Complex Objects  How to isolate the units of code? </li></ul>Using Test Doubles <ul><li>Test Doubles : </li></ul><ul><li>Dummy - objects are passed around but never actually used. Usually they are just used to fill parameter lists . </li></ul><ul><li>Fake - objects actually have working implementations, but usually take some shortcut which makes them not suitable for production </li></ul><ul><li>Stubs - provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. </li></ul><ul><li>Mocks - objects pre-programmed with expectations which form a specification of the calls they are expected to receive. </li></ul>
  6. 6. ____________________________ Styles What: Order : must fill itself from Warehouse If Warehouse contains the product quantity requested by Order: - Order becomes filled, Warehouse quantity adjusted. If Warehouse does not contain necessary quantity: -Order does not become filled. Warehouse unchanged. Example
  7. 7. ____________________________ Styles Regular test
  8. 8. ____________________________ Styles How do we test a mail was sent?! Stubs With a Stub :
  9. 9. “ Once,” said the Mock Turtle at last, with a deep sigh, “I was a real Turtle.” (Alice In Wonderland, Lewis Carroll) ____________________________ Styles Mocks
  10. 10. ____________________________ Styles Mock test: jMock
  11. 11. ____________________________ Styles We’re late!
  12. 12. ____________________________ Vocabulary “ The end justifies the means” State-based testing <ul><li>Traditional (Classical) Unit Testing . </li></ul><ul><li>Combines Stubs with Real Objects </li></ul><ul><li>Failing Tests – A collaborator may lead to multiple tests failed . </li></ul><ul><li>Unnecessary code – getters to expose state used only in tests. </li></ul><ul><li>Setup in a different place - a stub may be shared, it’s returned values not visible in the test </li></ul><ul><li>Sometimes stubs may be hard to implement by hand – how do we stub a HttpServletRequest ? A java.io.File? What do we put in constructors? </li></ul><ul><li>… </li></ul>
  13. 13. ____________________________ Vocabulary “ OOP should be Behavior Oriented Programming” Interaction-based testing <ul><li>verifies the behavior of a unit . </li></ul><ul><li>the only real class is the SUT (system under test) </li></ul><ul><li>Failing Tests – A problem at a Unit fails only Unit’s test. </li></ul><ul><li>Unnecessary code – don’t need state accessors. </li></ul><ul><li>Setup in setup - a stub returned values are visible in test. </li></ul><ul><li>Stubs easy to be fabricated. </li></ul><ul><li>In TDD -> leads to good design (Interface discovery, Tell Don’t Ask) </li></ul><ul><li>… . </li></ul>
  14. 14. ____________________________ Vocabulary What should I use? <ul><li>Some code is very stateful, and is best tested with a state-based test. . </li></ul><ul><li>Other code is more stateless and can be fully tested only with an interaction-based style </li></ul><ul><li>Design phase – mocks may help </li></ul><ul><li>Maybe a better question: </li></ul><ul><li>which style will be most effective for a particular piece of code?! </li></ul>
  15. 15. ____________________________ Vocabulary Good Unit Tests “ Uncle Bob” suggests the F.I.R.S.T acronym in “Clean Code” to describe what well written (clean) unit tests should look like : F ast - they should run quickly. This encourages you to run them often. (framework) I ndependent - they should not depend on each other. It should be possible to run the tests in any order. R epeatable - it should be possible to run them in any environment. They should be environment independent. S elf-Validating - they should either pass or fail. (framework) T imely - they should be written in a timely manner i.e. just before the production code is written.
  16. 16. ____________________________ Toys Mockito
  17. 17. questions? ____________________________ http :// martinfowler.com/articles/mocksArentStubs.html http://code.google.com/p/mockito/ http://xunitpatterns.com/Mocks,%20Fakes,%20Stubs%20and%20Dummies.html Martin Fowler: “ Refactoring . Improving the design of existing code ” http://blogs.thoughtworks.com/ Robert “Uncle Bob” Martin: “ Clean Code : A Handbook of Agile Software Craftsmanship” http://mockobjects.com/ http://benpryor.com/blog/2007/01/16/state-based-vs-interaction-based-unit-testing/

×