Top 12 Selected Unit Testing Best Practices

Google+ Facebook Twitter LinkedIn Dzone Reddit Digg Blogger Hacker News Addthis

By Markus Sprunck; Revision: 2.0; Status: final; Last Content Change: Aug 5, 2014; 

Writing just another best practice list about unit testing is easy, but working according these recommendations is a more difficult task. In the project described in A Library to Empirically Estimate Big-O Time Efficiency and Check Results of Analysis in JUnit Tests I followed my own tips. All the source code in this project has been developed with 100% TDD and applied most of the best practices. Just 'Use Parameterized Tests' and 'Structure All Test Cases' has not been applied, because it was not needed.

1) Always Write Isolated Test Cases

- The order of execution has to be independent between test cases
- This gives you the chance to rearrange the test cases in clusters (e.g. short-, long-running) and retest single test cases

2) Test One Thing Only in One Test Case

- Each test should focus on one aspect of an isolated method and/or class
- For integration test this goal may be difficult to achieve, but also here trying to focus on single aspects is helping

3) Use a Single Assert Method per Test Case

- Don't test too many different things at once! If you use just one Assert it's easier
- This increases the number of test cases and the single test better to maintain 
- If one or more tests fail, you may find the root cause in a easier way

4) Use a Naming Convention for Test Cases

- A convention like: <Method-Name Under Test>_<Scenario>_<Expected-Outcome>
- This helps to avoid comments and increases the maintainability

5) Use the AAA Pattern

- Use separated blocks ("ARRANGE", "ACT" and "ASSERT") help to improve maintainability
- Use comments to mark these blocks

6) Avoid the Expected Exception Tests

- Maybe @Test(expected = ArithmeticException.class) doesn't test what it claims to test
- Better when you measure the code coverage of test code   

7) Structure All Test Cases

- Like Short-/Long-Running, Integration-/Unit-tests, but don't use test suits to control order of execution

8) Use Descriptive Messages in Assert Methods

- Describe the WHY and not the WHAT, not like Assert.assertEquals( a, b, "must be equal")
- This helps to avoid to much comments in the test cases and increases the maintainability

9) Measure Code Coverage to Find Missing Test Cases

- Best indicator to find out what is not tested, but don't be to sure that the code works

10) Don't Forget to Refactor the Test Code

- Also maintain your test code (especially when after refactorings of the code under test)

11) Limit Use of Mocks

- In some cases absolutely necessary, but with better design stubs should be enough

12) Use Parameterized Tests

- They can help avoid code duplication and the best is the business gives you the data


All the best practices you may read here have been invented by someone else. I just collected all the good ideas (from books, trainings, blogs) and ranked them according my personal experiences. Please, do not hesitate to contact me if you have any ideas for improvement.

Change History

 Revision  Date  Author  Description
 1.0  Oct 21, 2013  Markus Sprunck  First version
 2.0 Aug 5, 2014 Markus Sprunck Add some additional explainations

Google+ Comments

You may press the +1 button to share and/or comment