While unit testing best practices are somewhat subjective, there are a number of the following standards most engineers agree upon:
- Test only one code unit at a time
- Mock out all the external components and dependencies
- Make each test independent of the others
- Avoid situations where tests must run in a specific order, or are otherwise interdependent
- Have one, and only one test to verify each behavior. It is counter-productive to have multiple test failures indicate the same defect.
- Only make one logical assertion per test. The assertion should, specifically, verify the expected behavior under test, not any additional observations about the code behavior
- Name unit tests clearly and consistently
- Avoid unnecessary preconditions ...