0%

Testing a Real Project · practice

Parametrize One Rule

The inclusive rule has three important positions: below the threshold, exactly on it, and above it. Copying a complete test three times would hide that all three examples ask one question.

Import pytest and replace the single equality test with one bounded marker recipe:

import pytest

from catalog import select_items


@pytest.mark.parametrize(
    ("quantity", "minimum", "kept"),
    [
        (3, 4, False),
        (4, 4, True),
        (5, 4, True),
    ],
)
def test_minimum_quantity_is_inclusive(quantity, minimum, kept):
    item = {"sku": "CASE", "quantity": quantity}

    selected = select_items([item], minimum)

    assert (selected == [item]) is kept

The @ line applies pytest’s supplied parametrize marker to this test . This lesson uses that one marker recipe; it does not teach how to author decorators or assume the function-as- material from Fundamentals II.

Run the suite:

python -m pytest -q

Pytest should collect three items from that function. Each row has the same meanings and the same assertion. A table mixing threshold behavior, CSV errors, and output formatting would save lines but erase the rule it is supposed to explain.

Keep a different promise separate

Add an ordinary test for order and duplicate preservation:

def test_selection_preserves_order_and_duplicates():
    first = {"sku": "B", "quantity": 8}
    repeated = {"sku": "A", "quantity": 8}
    items = [first, repeated, repeated]

    selected = select_items(items, 8)

    assert selected == [first, repeated, repeated]

This is not another value row for the scalar boundary rule. It protects a different collection-level promise, so a separate name makes a failure useful.

Name the tests and organize the assertions however reads best to you. The one thing that matters is that the parameter table holds genuinely different cases. A table with three rows that all say the same thing collects as three tests and catches exactly as much as one.

Task

In tests/test_catalog.py, use one pytest.mark.parametrize table with at least the below/equal/above cases for the inclusive minimum rule. Keep order and duplicate preservation in a separate ordinary test. The collected cases must pass the correct catalog and fail realistic threshold mutations.