Software Engineer

Case File: The Discount Tests

A discount rule decides what customers pay, and nobody dares to touch it. Instead of changing the code, you write the unit tests: functions that use assert. The checks run your tests on the real function and then on copies that each carry one deliberate bug, called mutants. A good suite fails on every mutant. The rule is generated for you alone.

50 min

Sabi Logistics · Engineering 1

The Discount Tests

The pricing code has no tests, and the last change cost us money without anyone noticing

Ifeoma Lead developer · Phone call

Ifeoma. Last month somebody changed “ten or more” to “more than ten” in the discount rule, and for three weeks every customer who bought exactly ten items paid too much. No test noticed, because there are no tests. The function is called final_price(unit_price, qty, member), and the rule is written in the code.

I do not want you to change it. I want you to protect it. Write unit tests, and I will check them the way a careful engineer would: I will break the function in small ways, one at a time, and see whether your tests notice. A test suite that passes on correct code proves little. A test suite that fails on every broken copy proves a lot.

The brief

What you need to know first.

  • Unit test. A function whose name starts with test_ and that calls the code and uses assert actual == expected. If the assertion is false, the test fails. Each test should check one thing and have a name that says what.
  • Work the expected value out by hand. Never copy the number that the function gives you: that only proves the function agrees with itself. Read the rule, work the answer out on paper, and write that number.
  • Boundaries are where bugs live. If a rule changes at 10 items, test 9, 10 and 11. If a limit is a cap, test an order that is exactly at the cap and one that goes over it. Test each rule alone and then together, and test a price where the division leaves a remainder (the rule rounds down).
  • Mutants. The checker runs your tests against the real function (all must pass) and then against copies with one deliberate bug each. A mutant that survives shows a gap in your tests. There are three groups: boundaries, arithmetic and logic. You are not told which mutant is which, so think about what could go wrong in every line.
  • The lab gives you the function in the starter, with one test as an example. Add your own tests below it.

The lab gives you the data for this case, invented and generated for you alone. Today is 2026-11-09.

Preparing your data…

On a real system

The lab modelled the rules with a script. These are the commands that you would type on the real thing.

You want toTypeWhat you get
run the tests of a Python projectpytest -qthe number of tests that passed and failed
measure which lines the tests runpytest --cov=app --cov-report=term-missingcoverage per file and the lines that no test reaches
check whether the tests would notice a bug (mutation testing)mutmut runchanges the code in small ways and reports the mutants that survive

Only run commands on systems that you own or have written permission to test.

🔒 The debrief unlocks when you close the case. Finish the lab above.

Write the test report

Write the note for the lead developer: the kinds of mistake that your tests can catch (boundaries, arithmetic, logic), which cases you chose and why, how you worked the expected values out (by hand from the rule, not from the output of the function), what a mutant that survives tells you, and why coverage alone does not show that tests are good.

0 words

🧪 Want to practise this on your own computer? See how to set up your lab →

📝

Knowledge Check

1 of 4

A test suite passes on the correct function and also on a copy with a deliberate bug. What does that tell you?

Report a problem

About: /lessons/case-the-discount-tests