0%

Chapter 10 · practice

Testing a Real Project

Install and Collect One Test

The catalog has survived real and failure-boundary reviews. What it does not have is a project-owned way to rerun those expectations after the next change. Start by making the test runner reproducible, just like Chapter 3 made a package reproducible.

Create a disposable environment and install the reviewed mirror pin:

python -m venv .venv
.venv/bin/python -m pip install pytest==9.1.1
nano requirements.txt
.venv/bin/python -m pytest --version

Put one line in the file, pytest==9.1.1, and save it. The last command should report pytest 9.1.1. The internal network reaches only Python Land’s package mirror, never public . .venv is derived state and is excluded when the workspace is saved. requirements.txt is the durable project decision another environment can rebuild.

Give pytest a name it collects

Create the test directory and open its first :

mkdir -p tests
nano tests/test_catalog.py

Write one test:

from catalog import select_items


def test_equal_quantity_is_included():
    item = {"sku": "EQ-4", "quantity": 4}

    assert select_items([item], 4) == [item]

Pytest discovers files named test_*.py and named test_*. The function takes no special base class. Its plain assertion is the same developer check you met in Fundamentals I, now collected and reported by a runner.

Run exactly this environment’s pytest:

.venv/bin/python -m pytest -q

One dot and 1 passed mean one test was collected and its assertion held. Silence from an assertion is not enough by itself: the pytest summary proves that the function was found.

Notice which of those two things is durable. .venv is derived state and will not survive the trip to the next lesson; requirements.txt and tests/test_catalog.py will. That split is the same one Chapter 3 made, now applied to the test runner instead of the application.

Pytest prints “no tests ran” after you accidentally name the file catalog_checks.py. What is the first contract to inspect?

Next you will separate a healthy collection from the several different ways a pytest command can stop.

Task

Create the exact durable requirements.txt, then create tests/test_catalog.py with one discoverable plain-assert test proving that an item equal to the minimum quantity is included. Run it with the pinned venv’s Python before submitting; the grader checks the files and collected behavior, not history or the excluded environment.