Testing · lesson 10 of 10
Ideas in this lesson
ABoundariesfrom lesson 6.3
BAcceptance testsfrom lesson 6.5
CMutanta copy of the code with one small mistake put in on purpose

Pila: the test suite

Last updated

Everything in Stage 6 comes together in one suite. You'll write the tests for Pila's queue rules: the boundaries, the acceptance criteria, and a regression test for the cancel bug. Then the page will test your tests. It hides five broken versions of queue_rules.py, each with one small mistake a real programmer could make, and runs your suite against each. A good suite fails on all five.

By the end of this lesson you can

  • Write a test suite from boundaries and acceptance criteria
  • Judge a suite by the bugs it catches, not by how many tests it has
  • Explain what mutation testing measures

What you’re making

A test suite for queue_rules.py, the file you wrote in lesson 5.P. It should check, as tests:

  • Opening hours at both edges: 6:59 and 7:00, 16:59 and 17:00 (lesson 6.3).
  • The daily limit: the 60th phone gets a number, the 61st doesn't.
  • The acceptance criteria: one number per phone, numbers in order (lesson 6.5).

Use @pytest.mark.parametrize where several cases share one test. Name each test so a failure reads as a sentence.

Testing the tests

How do you know a suite is good? Counting tests says little; lesson 6.9 showed coverage says little too. A better question: if someone made a small mistake in the code, would a test fail?

That's the idea behind mutation testing. A tool makes copies of the code, called mutants, each with one small change: < becomes <=, a + 1 disappears, a line is deleted. It runs the suite against each. A mutant the suite fails on is caught (testers say “killed”); one it passes on has survived, and shows a gap in the tests. Tools such as mutmut do this for Python projects automatically, making hundreds of mutants.

This page has five mutants of queue_rules.py, each a mistake from this course:

The five broken versions
MutantThe mistake
1Gives out a number at 17:00
2Refuses a number at 7:00
3Lets a 61st number through
4Gives a phone a second number
5Starts counting at 0

Write the suite

Write your tests in test_queue.py. Pressing Run here runs check_suite.py: it runs your tests against the real Pila first (they must all pass), then against each mutant, and says which it caught. The starter test catches one already. Keep adding tests until every mutant is caught.

Step 3 code
Edit the code, then run it again

This checker calls your test functions directly, once for each parametrize case; it doesn't support pytest fixtures, so build what each test needs inside it, as the lessons have.

The regression test

Lesson 6.7's exploring found that after a cancel, two patients could hold the same number, and lesson 6.9 said every fixed bug gets a test. Marco's fix works out the next number from a counter that only goes up, instead of from how many phones hold one. Here's the regression test that goes with it, written from Rina's steps:

def test_a_cancel_never_lets_two_phones_share_a_number():
    q = Queue()
    for i in range(5):
        q.take_number(f"0917 000 {i:04}", time(8, 0))
    q.cancel("0917 000 0001")
    q.take_number("0917 999 9999", time(8, 30))
    numbers = list(q.by_phone.values())
    assert len(numbers) == len(set(numbers))
This test reads q.by_phone directly. Lesson 6.5 warned against tests that know how the code is built. Is it a problem here?

Spot the trap: the suite that catches nothing

Spot the trapJun

Jun’s suite for queue_rules.py has 25 tests, all green, and 100% coverage. Run against the five mutants, it catches none of them.

What happens if…

Check what you learned

A mutant survives your suite. What does that tell you?
Which tests caught “Gives out a number at 17:00” and “Refuses a number at 7:00”?
What is the best measure of a test suite?

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