Unit Testing Best Practices
Learn the fundamental best practices when writing unit tests.
We'll cover the following
- Introduction
- Tests must exclude control structures
- Select a concise and consistent naming convention
- Ensure high test coverage
- Limit test coverage to non-trivial code
- Structure tests according to AAA principle
- Strive for one assertion per test
- Code coverage tools should be used
- Ensure test cases are rerun after every change
- Supplement your unit tests with other test types
- Conclusion
Introduction
Writing test cases is somewhat easier than writing application code. However, it is easy to forget the dos and don’ts when writing test code. Fortunately, many best practices are considered universal when writing test code. These practices are transferable and applicable from one test project to the next. We will look at the most important ones below. Each best practice has a quiz question to further test your understanding of that particular section.
Tests must exclude control structures
The application code handles control structures and the test code tests them. If the test code handled control structures, it too would be prone to failure. We do not want failure-prone tests because this would defeat the purpose of testing.
Get hands-on with 1400+ tech skills courses.