TDD is a cycle where you first write a test that fails, then write code to pass it, and finally refactor while keeping tests green.
Execution Sample
Software Engineering
def add(a, b):
return a + b
# Test: assert add(2, 3) == 5
This code defines a simple add function and a test that checks if adding 2 and 3 equals 5.
Analysis Table
Step
Action
Test Result
Code State
Next Step
1
Write test for add(2,3)==5
Fail (no code yet)
No add function
Write add function
2
Write add function returning a+b
Pass
add function implemented
Refactor if needed
3
Run test after refactor
Pass
Code clean and working
Write next test
4
Write test for add(-1,1)==0
Fail (new test)
Code unchanged
Write code to pass test
5
Update add function (if needed)
Pass
Function handles new case
Repeat cycle
6
Cycle continues for new features
Tests guide development
Code evolves safely
Continue TDD loop
💡 Testing stops when all tests pass and code is refactored cleanly for current features.
State Tracker
Variable
Start
After Step 2
After Step 5
Final
add function
undefined
defined as return a+b
same function, verified for new cases
stable and tested function
Key Insights - 3 Insights
Why do we write tests before writing the actual code?
Writing tests first ensures you know exactly what the code should do. As shown in execution_table step 1, the test fails initially because the code doesn't exist yet, guiding development.
What if the test never passes?
If the test never passes, it means the code doesn't meet the requirement. You must fix the code until the test passes, as shown in steps 2 and 5 where code changes lead to passing tests.
Why do we refactor after tests pass?
Refactoring cleans the code without changing behavior. Tests ensure the code still works after refactor, as seen in step 3 where tests pass after cleaning code.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the test first pass?
AStep 4
BStep 1
CStep 2
DStep 5
💡 Hint
Check the 'Test Result' column for the first 'Pass' entry.
According to variable_tracker, what is the state of the add function after step 5?
ASame function, verified for new cases
BUndefined
CDefined but untested
DRemoved
💡 Hint
Look at the 'After Step 5' column for 'add function' in variable_tracker.
If the test fails at step 4, what should be the next action according to the concept_flow?
AWrite a new test
BWrite code to pass the test
CRefactor code
DStop development
💡 Hint
Refer to the flow after 'Run Test -> Fails' which leads to 'Write Code to Pass Test'.
Concept Snapshot
Test-driven development (TDD) cycle:
1. Write a failing test.
2. Write code to pass the test.
3. Run tests to confirm pass.
4. Refactor code safely.
5. Repeat for new features.
Tests guide and protect code quality.
Full Transcript
Test-driven development (TDD) is a software practice where you first write a test that fails because the code does not exist yet. Then you write just enough code to make the test pass. After that, you refactor the code to improve its structure without changing behavior. You run tests after each step to ensure correctness. This cycle repeats for every new feature or change, ensuring code is always tested and reliable.