Introduction to TDD: What, Why, and How

Introduction to TDD

Welcome to the first lesson of our course on Test-Driven Development (TDD) using Python and pytest. TDD is an iterative software development process where tests are written before the actual code. This approach enables programmers to focus on the requirements before diving into implementation, ultimately leading to code that is more reliable and maintainable.

In this lesson, we will introduce you to the essential elements of TDD, including the Red-Green-Refactor cycle, which forms the backbone of this methodology. We'll also introduce the tools we'll be using: Python and pytest. Python is a popular programming language known for its readability and versatility, while pytest is a powerful testing framework for Python. pytest provides a simple yet comprehensive solution for defining and running tests. Let's begin by exploring TDD's core components with an example.

Writing the First Test (Red)

In TDD, the journey begins with writing a test that fails. This "Red" stage allows you to clarify your objective before implementation. Let's start by writing a test for a sum function, which will eventually add two numbers.

Create a file named test_math.py in the tests directory:

Python
from math_functions import sum

def test_sum():
    assert sum(2, 3) == 5

This test script:

  • Imports the sum function, which we haven't defined yet.
  • Contains a test function to ensure sum(2, 3) returns 5 using an assertion.

Run this test even without a sum function. It will fail, reflecting the "Red" phase, indicating the need for implementation.

Output:

collected 0 items / 1 error

=========================== short test summary info ============================
ERROR test_math.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.15s ===============================

This is an expected failure and shows that our test is working properly to identify unimplemented features.

Making the Test Pass (Green)

Our next goal is to write just enough code to make the test pass — the "Green" step. In TDD, this means implementing the simplest functionality that satisfies the test requirements. Let's define the sum function in a new file, math_functions.py:

Python
def sum(a, b):
    return 5

This solution might seem odd because it doesn't genuinely add two numbers. However, it exemplifies the TDD focus on passing the test with minimal implementation. By achieving this, we've fulfilled the current test condition.

Re-running our test will yield the following:

Output:

test_math.py .                                                       [100%]

============================== 1 passed in 0.05s ===============================

Seeing the test pass confirms that our code satisfies the current test scenario. We can later iterate on our implementation as new tests impose stricter functionality requirements.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal