Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

OS 4.1 (4 of 6): Test-driven and behaviour-driven design

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

Get the answers first

Imagine your teacher gives you the quiz answers before you study. You know exactly what to learn. You stop when you can answer everything.

Some developers work like this. They write the test first. Then they write the code. They stop when the test passes.

Key words

WordWhat it means
Unit testA test that checks one small part of the code.
TDDTest-driven development. Write a failing test first, then the code.
RefactoringMaking code cleaner without changing what it does.
BDDBehaviour-driven development. Describe the wanted behaviour in plain words before coding.
BehaviourWhat the software does when a user does something.
User storyOne need of a user, written in one sentence.
Acceptance criteriaThe checks that tell you the behaviour is done.
RequirementSomething the software must do.
Business valueWhat a behaviour is worth to the customer.

The cells students fill in are left blank in this preview.

TDD writes the test first

With TDD, each new feature starts with a test. The test says what the feature must do.

At first the test fails. That is good. It proves the test can fail. Then you write just enough code to make it pass.

Developers call a failing test red and a passing test green.

Check: red and green

Have a go

Check: red and green

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

The TDD cycle

flowchart TD
  add[1 Add a test for the new feature] --> red[2 Run all tests: the new one fails, red]
  red --> code[3 Write some code]
  code --> green[4 Run all tests: they pass, green]
  green --> clean[5 Refactor the code and run the tests again]
  clean --> again{More features?}
  again -->|yes| add
  again -->|no| done([Finished])

Fix the code until the tests pass

Code: python
def double(n):
    return n + 2   # this is wrong


def run_tests():
    assert double(4) == 8
    assert double(0) == 0
    assert double(10) == 20
    print("All tests pass: green")


run_tests()

# Run it. You see red: an error. Fix double() and run again.

Make a free account to save your work and track your progress.

Small steps

Tip

Write only one test at a time. Write only enough code to pass it. Small steps find mistakes while they are small and easy to fix.

BDD starts with the behaviour

BDD starts one step earlier. Before any test, people talk about the behaviour the user needs.

Customers, testers and developers all write the behaviour in plain words. Every behaviour must have business value. It must also link to a requirement.

Later, each behaviour becomes a test.

A behaviour specification

PartWhat to writeExample
TitleA short name for the behaviourStudent buys a day ticket
NarrativeAs a (who), I want (what), so that (why)As a student, I want a cheap day ticket, so that I save money
Acceptance criterion 1Given (start), when (action), then (result)Given I have a student card, when I choose a day ticket, then the price is 2 pounds
Acceptance criterion 2Given (start), when (action), then (result)Given I have no student card, when I choose a day ticket, then the price is 3 pounds

The cells students fill in are left blank in this preview.

Match the part

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

TDD and BDD side by side

TDDBDD
Starts withA small test for one unit of codeA behaviour the user needs
Written byDevelopersThe whole team, including customers
LanguageCodePlain words
Question it answersDoes this code work?Does the software do what the user needs?

The cells students fill in are left blank in this preview.

Code a bus fare from behaviours

A bus company gives you four behaviours in the code comments. Build the two functions. The hidden tests check each behaviour on its own, in the way TDD does. Press Run Tests. A test that fails tells you which behaviour is not done.

Code: python
def bus_fare(age, has_student_card):
    # The rules, as behaviours:
    # 1. A child under 5 travels free: give back 0.
    # 2. Anybody under 16 pays the child fare: 1 pound.
    # 3. A person of 16 or over with a student card pays 2 pounds.
    # 4. Everybody else pays the adult fare: 3 pounds.
    return None


def fare_message(age, has_student_card):
    # Give back text like: Your fare is 2 pounds. (A fare of 1 reads: Your fare is 1 pound.)
    # If the fare is 0, give back: You travel free.
    return None

Make a free account to save your work, or press Start lab to work through the whole lesson.

Write a behaviour specification

A college website lets a student reset a password. Write one behaviour specification. Give it a title. Write the narrative: As a ..., I want ..., so that ... Then write two acceptance criteria in the form Given ..., when ..., then ... One criterion is for a good case. One is for a problem case, such as a wrong email address. Say which requirement it links to.

A teacher marks this in a college. Nothing you write here is kept: it goes when you leave the page.

Make a free account to save your work, or press Start lab to work through the whole lesson.

Good or vague?

Have a go

Good or vague?

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Exit quiz

Have a go

Exit quiz

6 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.