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.
This gives you the chance to rearrange the test cases in clusters (e.g. short-, long-running) and retest single test cases.
For integration test this goal may be difficult to achieve, but also here trying to focus on single aspects is helping. This means that you usually have just one assert per test case. More than one asserts are always a hit that your test doesn't focus on one aspect or functionality.
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.
This helps to avoid comments and increases the maintainability and in the case a test fails you know faster what functionality has been broken.
Use separated blocks ("ARRANGE", "ACT" and "ASSERT") help to improve maintainability and use comments to mark these blocks. An alternative is the ("given", "when" and "then") style. This gives more structure to your unit tests.
This helps to avoid to much comments in the test cases and increases the maintainability.
Read More >