Bird
Raised Fist0
Testing Fundamentalstesting~6 mins

Test-driven development (TDD) concept in Testing Fundamentals - Full Explanation

Choose your learning style10 modes available
Introduction
Writing software can be tricky because bugs often appear after coding. Test-driven development helps prevent bugs by making tests before writing the actual code.
Explanation
Red Phase
In this first step, you write a test for a new feature or behavior that does not exist yet. Since the feature is not implemented, the test will fail, showing that the code needs to be written.
Writing a failing test first clarifies what the code should do.
Green Phase
Next, you write just enough code to make the failing test pass. The focus is on simplicity and correctness, not on perfect design or optimization at this stage.
Write minimal code to pass the test and confirm the feature works.
Refactor Phase
After the test passes, you improve the code's structure and readability without changing its behavior. The tests ensure that refactoring does not break the functionality.
Refactor safely with tests to keep code clean and maintainable.
Cycle Repetition
You repeat the red-green-refactor cycle for every new feature or bug fix. This iterative process builds a reliable and well-tested codebase over time.
Continuous cycles of testing and coding build quality software.
Real World Analogy

Imagine building a puzzle where you first decide the shape of the next piece (test), then cut the piece to fit (code), and finally smooth the edges to make it look nice (refactor). You repeat this for every piece until the puzzle is complete.

Red Phase → Deciding the shape of the next puzzle piece before cutting
Green Phase → Cutting the puzzle piece to fit the shape exactly
Refactor Phase → Smoothing the edges of the puzzle piece to improve appearance
Cycle Repetition → Repeating the process for each puzzle piece until the puzzle is done
Diagram
Diagram
┌───────────┐   fail   ┌───────────┐   pass   ┌────────────┐
│ Write Test│────────▶│ Write Code│────────▶│ Refactor   │
│ (Red)     │         │ (Green)   │         │ Code       │
└───────────┘         └───────────┘         └────────────┘
       ▲                                         │
       │                                         │
       └─────────────────────────────────────────┘
The cycle of writing a failing test, coding to pass it, and refactoring the code.
Key Facts
Test-driven development → A software practice where tests are written before the code they verify.
Red phase → Writing a test that fails because the feature is not implemented yet.
Green phase → Writing code to make the failing test pass.
Refactor phase → Improving code structure without changing its behavior after tests pass.
Cycle repetition → Repeating the red-green-refactor steps for each new feature or fix.
Code Example
Testing Fundamentals
import unittest

def add(a, b):
    return a + b

class TestAddFunction(unittest.TestCase):
    def test_add_positive(self):
        self.assertEqual(add(2, 3), 5)

    def test_add_negative(self):
        self.assertEqual(add(-1, -1), -2)

if __name__ == '__main__':
    unittest.main()
OutputSuccess
Common Confusions
Believing tests are written after coding is complete.
Believing tests are written after coding is complete. In TDD, tests are written <strong>before</strong> the code to guide development and catch errors early.
Thinking the green phase requires perfect code.
Thinking the green phase requires perfect code. The green phase focuses on <em>just enough</em> code to pass tests, not on final design or optimization.
Assuming refactoring can break functionality.
Assuming refactoring can break functionality. Refactoring is safe in TDD because tests verify that behavior remains unchanged.
Summary
Test-driven development starts by writing tests before the actual code to prevent bugs early.
The process cycles through writing a failing test, coding to pass it, and then improving the code safely.
This method builds reliable, clean, and well-tested software step by step.

Practice

(1/5)
1. What is the main idea behind Test-driven development (TDD)?
easy
A. Write tests only for completed features
B. Write code first, then write tests after
C. Write tests before writing the actual code
D. Write tests only for bugs found in production

Solution

  1. Step 1: Understand TDD process

    TDD means writing tests before the actual code to guide development.
  2. Step 2: Compare options with TDD definition

    Only Write tests before writing the actual code correctly states writing tests before code.
  3. Final Answer:

    Write tests before writing the actual code -> Option C
  4. Quick Check:

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

Solution

  1. Step 1: Recall the Red-Green-Refactor cycle

    The TDD cycle is: write a failing test (Red), write code to pass test (Green), then refactor.
  2. Step 2: Match cycle to options

    Write test, write code, refactor matches the correct order: write test, write code, refactor.
  3. Final Answer:

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

    TDD cycle = Test, Code, Refactor [OK]
Hint: Think: Red (test), Green (code), Refactor [OK]
Common Mistakes:
  • Writing code before tests
  • Refactoring before writing code
  • Mixing up the order of steps
3. Consider this TDD workflow:
1. Write a test that fails
2. Write code to pass the test
3. Refactor the code
4. Repeat for next feature

What will happen if you skip step 1 and write code first?
medium
A. Tests will automatically pass without issues
B. The code will be bug-free by default
C. Refactoring will be unnecessary
D. You may write code that does not meet requirements

Solution

  1. Step 1: Understand the role of failing tests in TDD

    Failing tests define what the code should do. Skipping them risks missing requirements.
  2. Step 2: Analyze consequences of skipping step 1

    Without tests first, code may not meet expected behavior, causing bugs or missing features.
  3. Final Answer:

    You may write code that does not meet requirements -> Option D
  4. Quick Check:

    Skip test first = Risk wrong code [OK]
Hint: Tests guide code; skip tests, code may miss goals [OK]
Common Mistakes:
  • Assuming code is always correct without tests
  • Thinking refactoring fixes missing tests
  • Believing tests are optional in TDD
4. You wrote a test first but it never fails when you run it initially. What is the likely problem?
medium
A. The test is not testing anything or is incorrect
B. The code is perfect and needs no changes
C. Refactoring was done before writing the test
D. The test runner is broken and skips tests

Solution

  1. Step 1: Understand expected test behavior in TDD

    Tests should fail first to show missing functionality.
  2. Step 2: Interpret why test never fails initially

    If test never fails, it likely does not check anything meaningful or has errors.
  3. Final Answer:

    The test is not testing anything or is incorrect -> Option A
  4. Quick Check:

    Failing test first means test must fail initially [OK]
Hint: Failing test first means test must fail initially [OK]
Common Mistakes:
  • Assuming code is already perfect
  • Ignoring test correctness
  • Blaming test runner without checking test code
5. You are using TDD to develop a feature that calculates discounts. You write a test for a 10% discount but forget to handle zero or negative prices. What is the best TDD approach to fix this?
hard
A. Ignore zero and negative prices as invalid inputs
B. Write new failing tests for zero and negative prices, then update code
C. Refactor existing test to include zero and negative prices without new tests
D. Write code first to handle zero and negative prices, then write tests

Solution

  1. Step 1: Identify missing test cases in TDD

    TDD requires writing tests for all expected cases, including edge cases like zero and negative prices.
  2. Step 2: Apply TDD cycle to add missing cases

    Write new failing tests for these cases, then write code to pass them, then refactor.
  3. Final Answer:

    Write new failing tests for zero and negative prices, then update code -> Option B
  4. Quick Check:

    Add tests for new cases before coding [OK]
Hint: Add tests for all cases before coding [OK]
Common Mistakes:
  • Coding before writing tests for new cases
  • Ignoring edge cases in tests
  • Trying to refactor tests without adding new ones
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