Unit testing Vocabulary, styles, toys ____________________________
____________________________ 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
____________________________ 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.
____________________________ 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
____________________________ 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>
____________________________ 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
____________________________ Styles Regular test
____________________________ Styles How do we test a mail was sent?! Stubs With a Stub :
“ Once,” said the Mock Turtle at last, with a deep sigh, “I was a real Turtle.” (Alice In Wonderland, Lewis Carroll) ____________________________ Styles Mocks
____________________________ 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>
____________________________ 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>
____________________________ 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>
____________________________ 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.