- You may not write production code until you have written a failing unit test
- You may not write more of a unit test than is sufficient to fail, and not compiling is failing
- you may not write more production code than i sufficient to pass the currently failing tests
The dirtier the tests, the harder they are to change.
The problem is that tests must change as the production code evolves.
From release to release the cost of maintaining test suite rose. When managers asked why their estimates were getting so large, the developers blame the tests. Without a test suite they lost the ability to make sure that changes their code base worked as expected.
Test code is just as important as production code
- Tests are what keep your production code flexible, maintainable and reusable.
- Tests allow you to make changes to code without fear.
- Tests enable change.
- Tests enable improving architecture.
Without tests, your code base rots.
- Build – builds up the test data
- Operate – works on the test data
- Check – ensure the operation yielded the expected results
Make tests very easy to read"
- Given, When, Then naming
More important than a single assert per test
Ensures that your tests are laser focused and not testing miscellaneous (non-related) things
tests should be fast / run quickly
tests should NOT depend on each other – tests should be able to be run in any order they like
they should be repeatable in ANY environment without need for any specific infrastructure
they should have a boolean output – pass or fail, nothing else
they need to be written in a timely fashion – just before the production code is written – ensures they are easy to code against