TDD
Test-driven development (TDD) is a way to build software in small steps: first write an automated test for one new behavior, then add enough code to make that test pass.
Also known as: Test-Driven Development, разработка через тестирование
What TDD changes
In test-driven development, a test is the starting point for a code change, not just a check you add afterward. You pick one behavior, express it as an automated test, and use the result to guide your next edit. Writing the test first makes you decide what a caller should get back or observe before you decide how the code will do it.
The red–green–refactor loop
- Red: Write a test for behavior the code doesn't support yet. Run it and confirm that it fails for the reason you expected. A broken test setup tells you nothing about whether the behavior is missing.
- Green: Make the smallest implementation change that satisfies the new test without breaking existing ones. Don't implement additional cases just because you can imagine needing them.
- Refactor: Improve the structure of the implementation or the tests while keeping the behavior the same. For example, rename a misleading helper or remove duplication. Rerun the tests to verify that the cleanup didn't change the result. If nothing needs cleanup, move on to the next behavior.
Example: a shipping threshold
A shop charges $5 for shipping but waives the fee on orders of $50 or more. Suppose shipping_fee already returns 500 cents for every order and an existing test covers a small order. The next test pins down what happens at exactly $50:
def test_shipping_is_free_at_fifty_dollars():
assert shipping_fee(subtotal_cents=5000) == 0
The first run returns 500 instead of 0. Add a branch that returns 0 when subtotal_cents >= 5000, then check that both the new test and the existing test pass. A useful next case is 4999 cents: it catches an implementation that accidentally puts the free-shipping boundary one cent too low.
When it helps and what it doesn't prove
TDD is a good fit for rules you can state with concrete examples, such as pricing, validation, or workflow transitions. Developers often use unit tests because they return feedback quickly. An integration test can also drive a change when the behavior only exists at the boundary between components. But relying on slow end-to-end tests for every small edit makes the loop harder to sustain.
Prefer checking the result a caller can observe over checking private helper calls. Otherwise, a harmless refactor can break a test even though the code still works. And passing tests—even with high code coverage—don't prove that you've chosen the right examples or captured every requirement. TDD doesn't replace broader testing.
How it comes up in interviews
In a live coding exercise, an interviewer may ask you to add a feature or fix a bug using TDD. Show the first failing test and explain what it tells you before changing the implementation. A focused case with a clear expected result is more useful than a large test that fails for several possible reasons.