TDD Flashcards
Why use TDD?
- Improved test coverage leads to 40% - 80% fewer bugs in production (comparable to alternatives like design review, code review, and lint tools; much better than static types)
- Eradicates fear of change => Smooth CI/CD
- Fast developer feedback loop
What is the TDD process?
- Red - Before writing implementation code, write a test and watch it fail
- Green - Write the smallest amount of code required to make the test pass, and watch the test pass
- Refactor - Refactor if needed; the test will help you refactor with confidence
Why write tests first?
- Encourages loose coupling – more modular architecture
- Think about APIs before implementation, leading to better developer experience
- Form a hypothesis first, removing hindsight bias
Explain RITE way
R - Readable (did I answer the 5 questions?)
I - Isolated/Integrated
Isolated code under test, isolated tests from each other. Tests should be able to run in any order, and in parallel.
Integrated - Unit of code under test needs to be tested in integration with other units of code (and in the context of the app)
T - Thorough - test likely edge cases (zero, one, range boundaries, interfaces)
E - Explicit - Everything you need to know to understand a test is defined inside the test.
What are the 5 questions every unit test must answer?
- What component?
- What is the behavior under test? (human-readable prose description)
- Actual result?
- Expected result?
- How do you reproduce if the test fails? (Sanity check - did I answer the other questions well?)
What is the difference between unit tests and integration tests?
Unit - Tiny, fast, cheap (good foundation), but if you’re not testing integrations, you’re not testing integrations.
Integration - Big, slow, clunky, expensive