Sub topic 5
Covering edge cases with parametrized tests
This chapter assumes you can run pytest, read a @pytest.mark.parametrize decorator, and have already generated a first set of unit tests from a prompt that includes the code and project conventions. Here you will ask the assistant for edge cases on purpose and check that the parametrized tables it writes are complete and correct.
Why edge cases need to be requested
An assistant tends to produce the "happy path" first: a few typical inputs with obvious results. Bugs usually sit at boundaries, where a comparison flips or a branch changes. If your prompt does not name those boundaries, the output often skips them.
Parametrization helps because each row of a table is one case. A reviewer can read the table and see at a glance which cases exist and which are missing.
Find the boundaries from the contract
Start from the documented behavior of the function. Here is the running example.
def apply_discount(price: float, percent: float) -> float:
"""Return price reduced by percent, rounded to two decimals.
Raises ValueError if price is negative or percent is outside 0-100.
"""
if price < 0:
raise ValueError("price must be non-negative")
if not 0 <= percent <= 100:
raise ValueError("percent must be between 0 and 100")
return round(price * (1 - percent / 100), 2)The contract gives you these boundaries:
| Input | Valid edge | Just outside |
|---|---|---|
| `percent` | 0 and 100 | -5, 101 |
| `price` | 0 | -1 |
| Result | rounded to two decimals | not applicable |
Ask the assistant to cover each row. A prompt line such as "Cover normal values, the boundaries 0 and 100 for percent, and every ValueError path" works because it names the cases instead of saying "be thorough".
A table the assistant might produce
import pytest
from pricing import apply_discount
@pytest.mark.parametrize(
"price, percent, expected",
[
(100, 10, 90.0),
(80, 25, 60.0),
(19.99, 0, 19.99),
(50, 100, 0.0),
],
)
def test_apply_discount_returns_expected_price(price, percent, expected):
assert apply_discount(price, percent) == expected
@pytest.mark.parametrize("price, percent", [(-1, 10), (10, -5), (10, 101)])
def test_apply_discount_rejects_invalid_input(price, percent):
with pytest.raises(ValueError):
apply_discount(price, percent)The success cases and the error cases are in separate tests. That split is deliberate. The two groups need different assertions, and a failing row in either one is easy to locate.
Check the table, not just the result
A passing run does not prove the table is complete. Review it against the boundary list:
- Percent 0 is present:
(19.99, 0, 19.99). - Percent 100 is present:
(50, 100, 0.0). - Percent just below 0 and just above 100 are present:
-5and101. - Negative price is present:
-1. - Price 0 is missing. It is a valid edge and sits right next to the
price < 0check. - Rounding is missing. Every expected value is already a clean number, so a bug in
roundwould go unnoticed.
Ask for the gaps in a follow-up message, for example: "Add a row with price 0, and a row where the discounted price needs rounding. Derive the expected value by hand and show the arithmetic."
@pytest.mark.parametrize(
"price, percent, expected",
[
(0, 50, 0.0),
(10, 33, 6.7),
],
)
def test_apply_discount_edge_rows(price, percent, expected):
assert apply_discount(price, percent) == expectedFor (10, 33, 6.7): 10 × 0.67 = 6.7, which needs no rounding beyond the float representation. A better rounding row is (10, 33.333, 6.67), because 10 × 0.66667 = 6.6667, which rounds to 6.67. Check each derived value yourself before you accept it.
Readable failures with ids
When a table grows, pytest names failures by index, such as [3-50-100]. Give each row an id so a failure names the case.
import pytest
from pricing import apply_discount
@pytest.mark.parametrize(
"price, percent, expected",
[
pytest.param(19.99, 0, 19.99, id="zero-percent"),
pytest.param(50, 100, 0.0, id="full-discount"),
pytest.param(0, 50, 0.0, id="zero-price"),
pytest.param(10, 33.333, 6.67, id="needs-rounding"),
],
)
def test_apply_discount_named_cases(price, percent, expected):
assert apply_discount(price, percent) == expectedCommon errors
- Expected values copied from the function's output. If you ran the code and pasted the result, the test only repeats the current behavior. Derive values from the docstring.
- One giant table mixing success and error rows. The error rows need
pytest.raises, so keep them in a separate test. - Float equality surprises. Exact
==comparison works here because the function rounds. For unrounded float math, usepytest.approx. pytest.raises(ValueError)with no message check. It passes if the wrong branch raises. Addmatch="percent"when the branch matters.
Exercise
The error test uses rows (-1, 10), (10, -5), and (10, 101). Which input is not covered, and how would you make the test confirm which branch raised?
Show answer
Price 0 is a valid edge and isn't tested, and no row checks which ValueError branch fired. For the second point, add match to pytest.raises, for example match="price" for the price row and match="percent" for the percent rows, passing the expected message as an extra parameter in the table.