Python Testing - Given When Then syntax

TDD tip:

You can add GIVEN, WHEN, THEN as a docstring to your tests to better express your intent.

An example👇

def test_all(self):
    GIVEN a PhoneBook with a records property
    WHEN the all method is called
    THEN all numbers should be returned in ascending order

    phone_book = PhoneBook(
            ("John Doe", "03 234 567 890"),
            ("Marry Doe", "01 234 567 890"),
            ("Donald Doe", "02 234 567 890"),

    previous = ""

    for record in phone_book.all():
        assert record[0] > previous
        previous = record[0]

Test-diven devopment and pair programming

TDD Tip:

When practicing Test-driven Development with pair programming, try having one developer write the failing test while the other writes the code to get it to pass. The first developer is the responsible for any refactoring.


  1. It's fun.
  2. This can accelerate the learning of a less experienced developer when paired with a more experienced developer.

For more TDD benefits, check out What is Test-Driven Development? .