Tests as a Safety Net
About this app
A discount function with tiers (5 % from €100, 10 % from €500) and six pytest tests. Seven changes are applied, with the old line struck through. The tests run row by row with traffic lights, E lines and a pytest-style summary. Each change drops into the net as a ball: it is caught when a test turns red, otherwise it falls through. Two broken discount tiers slip past green tests until the missing boundary test rabatt(500) == 50 is added. Plus a refactoring that rightly stays green.
What tests do
A unit test compares a function’s result with an expectation from the requirement. The arrange, act, assert pattern describes its structure: set up the situation, call the function, check the result, for example assert rabatt(200) == 10. Green only means that what is checked is correct. Bugs often sit at boundaries, so edge cases belong in the tests: a test exactly at the boundary, such as rabatt(500) == 50, catches > instead of >=. As regression tests, all tests run again after every change so that whatever worked keeps working.
What the app shows
On the left is rechnung.py with a discount scale: 5 % from €100, 10 % from €500. On the right are six tests from test_rechnung.py. Seven changes are applied one after another, with the old line struck through and the new one below. The tests then run line by line with dots, F and E lines as in pytest. Each change drops into the net as a ball: it is caught if at least one test fails, otherwise it falls through. Two broken discount levels go unnoticed despite green tests.
Finding the missing test
The exercise asks which seventh test is missing. You pick a candidate and all changes run again. The boundary test rabatt(500) == 50 catches both bugs that slipped through. Other candidates such as rabatt(999) catch only one or check boundaries that are already covered. More tests are not automatically better; what matters is what they check. A refactoring of the scale into a table rightly stays green: tests do not prevent refactoring, they protect it.
Frequently asked questions
- What is a unit test?
- A unit test checks a small unit of a program, usually a function, against an expected result. It runs automatically and reports whether the expectation holds. In Python such tests are often written with pytest as functions containing assert statements.
- Why should you test edge cases?
- Many bugs arise at boundaries, for example > instead of >= or an off-by-one error. Tests with values far from the boundary do not notice. A test exactly on the boundary and just beside it reliably reveals such bugs.
- Do passing tests mean the code is bug-free?
- No. Passing tests only show that the checked cases are correct. Bugs in areas no test covers go unnoticed. What matters is that the tests hit the important cases and boundaries of the requirement.
- Does the app run real pytest?
- There is no pytest in the browser; the app reproduces pytest’s flow and output. A code window offers a runnable snippet to copy: the function with the chosen change, the tests as functions and a loop that calls them.
Subject: Software engineering
More from Software engineering
- Class Diagram and Code in Sync
- Token Game in Activity Diagrams
- Trust, but Verify
- Waterfall or Agile?
- WIP Limit and Lead Time
A VisuApp by heyprof: interactive, free in your browser, no sign-up. What is a VisuApp? · All apps