Test-driven development (TDD) in Software Engineering - Time & Space Complexity
When using Test-driven development (TDD), we write tests before code. Understanding time complexity here means looking at how the number of tests and code changes affect the time spent developing and running tests.
We want to know: How does the effort grow as the project or test suite grows?
Analyze the time complexity of running tests in a TDD cycle.
for each new feature:
write a failing test
write code to pass the test
run all tests
if tests pass:
proceed to next feature
else:
fix code and rerun tests
This snippet shows the basic TDD loop: write a test, write code, run all tests, and repeat until all tests pass.
In this TDD process, the main repeating operation is running all tests after each code change.
- Primary operation: Running the full test suite
- How many times: Once after every code change or test addition
As the number of tests grows, running all tests takes more time each cycle.
| Input Size (number of tests) | Approx. Operations (test runs) |
|---|---|
| 10 | Runs 10 tests each cycle |
| 100 | Runs 100 tests each cycle |
| 1000 | Runs 1000 tests each cycle |
Pattern observation: The time to run tests grows roughly in direct proportion to the number of tests.
Time Complexity: O(n)
This means the time to run tests grows linearly with the number of tests in the suite.
[X] Wrong: "Running tests after each change takes the same time no matter how many tests exist."
[OK] Correct: As tests increase, running all tests takes longer, so the time grows with the number of tests.
Understanding how test suite size affects development time shows you think about practical software quality and efficiency. This skill helps you balance writing enough tests without slowing down progress.
"What if we only ran the tests related to the changed code instead of the full suite? How would the time complexity change?"
Practice
Solution
Step 1: Understand TDD process
TDD means writing tests before the actual code to guide development.Step 2: Identify the main goal
The main goal is to ensure code correctness by writing tests first.Final Answer:
To write tests before writing the actual code -> Option AQuick Check:
TDD = Write tests first [OK]
- Thinking code is written before tests
- Confusing TDD with writing documentation
- Believing tests are optional in TDD
Solution
Step 1: Recall TDD cycle steps
The TDD cycle is: write a failing test, write code to pass it, then refactor.Step 2: Match the correct sequence
Write test, write code, refactor matches this order exactly.Final Answer:
Write test, write code, refactor -> Option BQuick Check:
TDD cycle = Test -> Code -> Refactor [OK]
- Writing code before tests
- Refactoring before writing code
- Mixing the order of steps
1. Write a test that expects function add(2, 3) to return 5.
2. Implement add function as def add(a, b): return a - b
3. Run the test.What will be the test result?
Solution
Step 1: Analyze the function implementation
The function subtracts b from a instead of adding them.Step 2: Compare expected and actual results
Calling add(2, 3) returns -1, but test expects 5, so test fails.Final Answer:
Test fails because the function returns wrong result -> Option CQuick Check:
Wrong function logic = Test fails [OK]
- Assuming test passes despite wrong logic
- Confusing syntax error with logic error
- Thinking tests skip automatically
Solution
Step 1: Understand TDD failing test requirement
In TDD, the first test should fail because code is not implemented yet.Step 2: Identify why test never fails
If test never fails, it likely does not check the feature properly or has wrong assertions.Final Answer:
Test is not written correctly to check the feature -> Option AQuick Check:
Failing test means correct test setup [OK]
- Assuming code is perfect without implementation
- Blaming test framework without checking test code
- Forgetting to run tests
Solution
Step 1: Recall TDD workflow
TDD requires writing a failing test first, then code to pass it, then refactoring.Step 2: Match steps to TDD
Write a test for filtering evens, write code to pass test, then refactor code follows this exact sequence for developing the function.Final Answer:
Write a test for filtering evens, write code to pass test, then refactor code -> Option DQuick Check:
TDD = Test -> Code -> Refactor [OK]
- Writing code before tests
- Writing tests after code completion
- Mixing test and code writing steps
Ready to prove it?
Pass the test and this pattern moves unknown
