0
0
Testing Fundamentalstesting~6 mins

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

Choose your learning style9 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 developmentA software practice where tests are written before the code they verify.
Red phaseWriting a test that fails because the feature is not implemented yet.
Green phaseWriting code to make the failing test pass.
Refactor phaseImproving code structure without changing its behavior after tests pass.
Cycle repetitionRepeating 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.