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
| Word | What it means |
|---|---|
| Unit test | A test that checks one small part of the code. |
| TDD | Test-driven development. Write a failing test first, then the code. |
| Refactoring | Making code cleaner without changing what it does. |
| BDD | Behaviour-driven development. Describe the wanted behaviour in plain words before coding. |
| Behaviour | What the software does when a user does something. |
| User story | One need of a user, written in one sentence. |
| Acceptance criteria | The checks that tell you the behaviour is done. |
| Requirement | Something the software must do. |
| Business value | What 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 questionsThe 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
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
| Part | What to write | Example |
|---|---|---|
| Title | A short name for the behaviour | Student buys a day ticket |
| Narrative | As a (who), I want (what), so that (why) | As a student, I want a cheap day ticket, so that I save money |
| Acceptance criterion 1 | Given (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 2 | Given (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
| TDD | BDD | |
|---|---|---|
| Starts with | A small test for one unit of code | A behaviour the user needs |
| Written by | Developers | The whole team, including customers |
| Language | Code | Plain words |
| Question it answers | Does 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.
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 questionsExit quiz
Have a go