Testing · lesson 5 of 10
Ideas in this lesson
AGiven · When · Thenarrange · act · assert
BBehaviour, not implementationtest what it does, not how

Testing from acceptance criteria

Last updated

In lesson 3.P the team wrote acceptance criteria for Pila's first story with Kap. Ernesto: how the people paying for Pila will know it's done. Those sentences are already test cases, waiting to be typed. In this lesson you'll turn them into pytest tests, see why tests should check what the software does rather than how it does it, and meet behaviour-driven development, which builds a whole way of working on this idea.

By the end of this lesson you can

  • Turn Given–When–Then acceptance criteria into arrange–act–assert tests
  • Write tests that check behaviour, not how the code is built
  • Explain how tests act as documentation that can’t go out of date

Criteria are tests in waiting

Lesson 3.4 wrote acceptance criteria as Given–When–Then: given a situation, when something happens, then this should result. Lesson 6.2 wrote tests as arrange–act–assert. They're the same three parts with different names:

One shape, two vocabularies
Acceptance criterionTestPila’s “one number per phone”
GivenArrangeA phone already has number 1 today
WhenActThe same phone texts again
ThenAssertIt gets number 1 again

So the criteria Kap. Ernesto agreed to can become tests almost word for word, and a test's name can say the criterion in plain words.

Write the three tests

Each test has its criterion as comments, and the “Given” already done. Replace each assert False line with the “When” (a call to take_number) and the “Then” (an assert). Run until all three pass.

Step 2 code
Edit the code, then run it again

These tests are a kind of documentation that can't quietly go out of date: if Pila stops doing what a test name says, the test fails. A new teammate can read the test names to learn the rules.

What, not how

A good test checks what the software does, as someone using it would see it, and not the details of how it's built inside. Tests that know about the inside break whenever the code is tidied, though nothing a user sees has changed. Then people stop trusting them.

0 of 5 placed right

  1. assert q.take_number(phone, time(9, 0)) == first

  2. assert phone in q.by_phone

  3. assert gateway.sent[0][1].startswith("You are number")

  4. assert len(q.by_phone) == 60

  5. assert q.take_number(phone, time(17, 0)) is None

Behaviour-driven development

In the early 2000s, Dan North found that new programmers learning test-driven development (lesson 6.6) kept asking what to test, and what to call their tests. His answer was to talk about behaviour: name each test as a sentence about what the software should do, and write the scenarios with the people who want the software, in Given–When–Then. He called it behaviour-driven development (BDD).

Some teams use tools that run plain-language scenarios directly as tests, so the business can read and even write them. Pila's team keeps it simpler: criteria in Given–When–Then, agreed with Kap. Ernesto, and pytest tests named after them. The idea that matters is shared: the people who need the software and the people who build it agree on examples before the code is written.

Spot the trap: the test that knows too much

Spot the trapJun

Jun tests “one number per phone” with: take a number, then assert len(q.by_phone) == 1. It passes. Next month Marco changes Queue to keep tickets in a list, for the waiting-room screen.

What happens if…

Check what you learned

In a test, which part does “When the same phone texts again” become?
Why should tests check behaviour rather than implementation?
What did Dan North’s behaviour-driven development add to testing?

Sources

Every claim on this page comes from these sources: the standard, the founding text of a method, a study, or the official record of a project, from the body that publishes it. Industry reports are labelled as such.

All sources for Simple Software Development