TDD Flashcards

1
Q

Why use TDD?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the TDD process?

A
  1. Red - Before writing implementation code, write a test and watch it fail
  2. Green - Write the smallest amount of code required to make the test pass, and watch the test pass
  3. Refactor - Refactor if needed; the test will help you refactor with confidence
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why write tests first?

A
  • Encourages loose coupling – more modular architecture
  • Think about APIs before implementation, leading to better developer experience
  • Form a hypothesis first, removing hindsight bias
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain RITE way

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the 5 questions every unit test must answer?

A
  1. What component?
  2. What is the behavior under test? (human-readable prose description)
  3. Actual result?
  4. Expected result?
  5. How do you reproduce if the test fails? (Sanity check - did I answer the other questions well?)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between unit tests and integration tests?

A

Unit - Tiny, fast, cheap (good foundation), but if you’re not testing integrations, you’re not testing integrations.

Integration - Big, slow, clunky, expensive

How well did you know this?
1
Not at all
2
3
4
5
Perfectly