Chapter 9 - Unit Tests
- We should try to write clean tests, meaning that we should make the tests as easy to maintain as the functions they are testing. If the functions are changing as business needs evolve, so should the tests.
Test code is just as important as production code. It is not a second-class citizen. It requires thought, design, and care. It must be kept as clean as production code.
- Tests enable bold changes to the code base.
^ This is kinda obvious, but I'm keeping it here to remember myself.
- Clean tests should be as elegant as clean production code. They should be designed to be read.
- Tests should follow the BUILD-OPERATE-CHECK pattern.
- There are things you can do in a testing environment that you cannot in production. But they are not related to code cleanliness, but to resource usage.
- We should try to minimize the assert statements per test.
- We should have one concept per test.
F.I.R.S.T. rules:
- Fast: Tests should run fast, so people always want to run them.
- Independent: You should be able to run any test independently.
- Repeatable: Tests should run in any environment.
- Self-validating: Tests should have a boolean output, you should not have to think or investigate to know if it passed.
- Timely: Tests should be written just before production code.
Top comments (0)