Exubero - JUnit Anti-patterns
JUnit FAQ
Test Often.: JUnit Best Practices, Part 2 -- Keep 'em Separated
- The purpose of unit testing is to take the guesswork out of error location.
- Testing many different behaviors in a single test method is a common practice because it's so easy to do, but this is a clear case of being penny-wise and pound-foolish. Take the time to separate each behavior into its own unit test.
- when the method under test performs more complex tasks, it's tempting to add multiple assertions to your JUnit tests to verify several different behaviors at once. Despite appearances, however, this approach can actually increase your overall testing time and effort.
- JUnit tests are designed to fail as soon as the first assertion fails.
- Since your brain is required for each debug/fix effort, this process cannot be automated.
- A better approach would be to split this one test method into three tests, each with a single assertion
- True, your test suite has increased in size, from one test to three, but you've always had three tests all along anyway, and this change merely reorganizes your test suite to reflect that.
- More importantly, striving for one assertion per unit test streamlines the test-debug-retest cycle and improves the overall testing effort.
No comments:
Post a Comment