Test-driven development
The red, green, refactor cycle: write a failing test first, then the simplest code that passes it, then clean up; small steps, triangulation, what the method brings and what it does not guarantee.
- 26 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- The steps
- Scenarios
A few cards from the deck
Test-driven development: what is it?
Show the answer
A method where every piece of production code is preceded by an automated failing test that defines what the code must do.
The test comes before the code: it acts as an executable specification and forces thinking about the interface before the implementation.
This is the only test written so far. Which production code is the simplest, in the TDD sense, to make it pass?
```python
def test_prix_ttc():
assert prix_ttc(100) == 120`return 120return ht * 1.2return round(ht * 1.2, 2)return ht + ht * 20 / 100
Show the answer
return 120At green, write the minimum: a constant is enough. The next test (for example
prix_ttc(50) == 60) will force generalisation, by triangulation.The cycle runs in order: … (a test fails), … (the minimal code makes it pass), then … (clean up under the tests' protection).
Show the answer
The cycle runs in order: red (a test fails), green (the minimal code makes it pass), then refactor (clean up under the tests' protection).
Each step has one goal: prove, make it work, then improve. Mixing them loses the tests' safety net.
Red: what is it?
Show the answer
The first step of the cycle: write a test that fails, which proves it can detect the absence of the wanted behaviour.
A test that has never failed proves nothing: it might pass whatever is written. You must see it fail, for the right reason.
One turn of the TDD cycle: which step comes after "Write a test describing the wanted behaviour"?
- Write the minimal code that passes it
- Rerun all the tests and watch them pass
- Refactor the code and structure
- Run it and watch it fail
Show the answer
Run it and watch it fail
You first see the test fail, to know it detects something; refactoring comes only once the bar is green, and ends with another run.