Here’s a list of TDD best practices that I have been accumulating in my developer toolbox. Ping me if you think I’m missing anything, and I will add it to the list.
- Have separate source and test folders. Test code should follow the structure of source.
- Test should fail the first time it’s written/run
- Test names should reflect intent, and names should be expressive
- Refactor to remove duplicate code after passing test
- Re-run tests after every refactoring
- Only write new code when a test is failing. Each test should test new/different behavior.
- Write the assertion first
- Minimize the assertions in each test
- All tests should pass before writing the next test
- Only refactor when all tests are passing
- Write the simplest code to pass the test
- Don’t introduce dependencies between tests. Test should pass when run in any order.
- Tests should run fast. A slow test is a test that won’t get run.
- Use mock objects to test code at system boundaries (e.g. database, container, file system) so that tests run fast.