Bird
Raised Fist0
Software Engineeringknowledge~10 mins

Test-driven development (TDD) in Software Engineering - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Test-driven development (TDD)
Write a Test
↓
Run Test -> Fails
↓
Write Code to Pass Test
↓
Run Test -> Passes
↓
Refactor Code
↓
Run Test -> Passes
↓
Repeat for Next Feature
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
StepActionTest ResultCode StateNext Step
1Write test for add(2,3)==5Fail (no code yet)No add functionWrite add function
2Write add function returning a+bPassadd function implementedRefactor if needed
3Run test after refactorPassCode clean and workingWrite next test
4Write test for add(-1,1)==0Fail (new test)Code unchangedWrite code to pass test
5Update add function (if needed)PassFunction handles new caseRepeat cycle
6Cycle continues for new featuresTests guide developmentCode evolves safelyContinue TDD loop
💡 Testing stops when all tests pass and code is refactored cleanly for current features.
State Tracker
VariableStartAfter Step 2After Step 5Final
add functionundefineddefined as return a+bsame function, verified for new casesstable 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.

Practice

(1/5)
1. What is the main purpose of Test-driven development (TDD)?
easy
A. To write tests before writing the actual code
B. To write code first and then tests later
C. To avoid writing any tests
D. To write documentation before coding

Solution

  1. Step 1: Understand TDD process

    TDD means writing tests before the actual code to guide development.
  2. Step 2: Identify the main goal

    The main goal is to ensure code correctness by writing tests first.
  3. Final Answer:

    To write tests before writing the actual code -> Option A
  4. Quick Check:

    TDD = Write tests first [OK]
Hint: Remember: TDD starts with tests, not code [OK]
Common Mistakes:
  • Thinking code is written before tests
  • Confusing TDD with writing documentation
  • Believing tests are optional in TDD
2. Which of the following is the correct order of steps in the TDD cycle?
easy
A. Write code, write test, refactor
B. Write test, write code, refactor
C. Refactor, write code, write test
D. Write test, refactor, write code

Solution

  1. Step 1: Recall TDD cycle steps

    The TDD cycle is: write a failing test, write code to pass it, then refactor.
  2. Step 2: Match the correct sequence

    Write test, write code, refactor matches this order exactly.
  3. Final Answer:

    Write test, write code, refactor -> Option B
  4. Quick Check:

    TDD cycle = Test -> Code -> Refactor [OK]
Hint: TDD always starts with a failing test [OK]
Common Mistakes:
  • Writing code before tests
  • Refactoring before writing code
  • Mixing the order of steps
3. Consider this TDD scenario:
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?
medium
A. Syntax error in function definition
B. Test passes successfully
C. Test fails because the function returns wrong result
D. Test is skipped automatically

Solution

  1. Step 1: Analyze the function implementation

    The function subtracts b from a instead of adding them.
  2. Step 2: Compare expected and actual results

    Calling add(2, 3) returns -1, but test expects 5, so test fails.
  3. Final Answer:

    Test fails because the function returns wrong result -> Option C
  4. Quick Check:

    Wrong function logic = Test fails [OK]
Hint: Check if function logic matches test expectation [OK]
Common Mistakes:
  • Assuming test passes despite wrong logic
  • Confusing syntax error with logic error
  • Thinking tests skip automatically
4. You wrote a test first but it never fails even before implementing the code. What is the likely problem?
medium
A. Test is not written correctly to check the feature
B. Code is already perfect and needs no changes
C. Test framework is broken and skips tests
D. You forgot to run the test

Solution

  1. Step 1: Understand TDD failing test requirement

    In TDD, the first test should fail because code is not implemented yet.
  2. Step 2: Identify why test never fails

    If test never fails, it likely does not check the feature properly or has wrong assertions.
  3. Final Answer:

    Test is not written correctly to check the feature -> Option A
  4. Quick Check:

    Failing test means correct test setup [OK]
Hint: First test must fail to guide development [OK]
Common Mistakes:
  • Assuming code is perfect without implementation
  • Blaming test framework without checking test code
  • Forgetting to run tests
5. You want to use TDD to develop a function that returns only even numbers from a list. Which sequence of steps best follows TDD principles?
hard
A. Write code and tests simultaneously, then refactor
B. Write code to filter evens, then write tests to check it
C. Write tests after finishing all code
D. Write a test for filtering evens, write code to pass test, then refactor code

Solution

  1. Step 1: Recall TDD workflow

    TDD requires writing a failing test first, then code to pass it, then refactoring.
  2. 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.
  3. Final Answer:

    Write a test for filtering evens, write code to pass test, then refactor code -> Option D
  4. Quick Check:

    TDD = Test -> Code -> Refactor [OK]
Hint: Always start with a test that fails for new feature [OK]
Common Mistakes:
  • Writing code before tests
  • Writing tests after code completion
  • Mixing test and code writing steps
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard