Sub topic 3
Running pytest and reading the output
This chapter assumes you can write Python functions, use a terminal with a virtual environment, and have pytest installed with pip install pytest. It also assumes you have written a test file or two. Here you will run pytest, choose which tests run, and read what it prints.
Running the tests
Run pytest from your project root. It looks for files named test_*.py or *_test.py, collects functions named test_*, and runs them. The same command works for a single file, a directory, or one test:
pytest
pytest test_cart.py
pytest test_cart.py::test_total_with_taxThe :: syntax is a node ID. Every test has one, and pytest prints it for failures.
Reading a passing run
Suppose test_cart.py has three tests and all pass. The output looks like this:
============ test session starts ============
collected 3 items
test_cart.py ... [100%]
============ 3 passed in 0.02s ============Each character after the file name is one test result. The most common ones are in the table.
| Symbol | Meaning |
|---|---|
| `.` | passed |
| `F` | failed, meaning an assertion did not hold |
| `E` | error, meaning a problem outside the test body, such as a fixture raising |
| `s` | skipped |
| `x` | xfail, an expected failure |
Reading a failure
When an assertion fails, pytest shows the source of the test, the values involved, and the line that failed. For example, if total([10, 20]) returned 31:
def test_total_without_tax():
> assert total([10, 20]) == 30
E assert 31 == 30
E + where 31 = total([10, 20])
test_cart.py:5: AssertionError
========= short test summary info =========
FAILED test_cart.py::test_total_without_tax - assert 31 == 30The > marks the failing line. Lines starting with E explain the failure. The short summary at the end lists each failed node ID, so you can copy one to rerun that test alone.
Useful options
These are the command lines you will use most often:
pytest -v
pytest -k total --maxfail=1
pytest -m "not slow"-vprints one line per test with its node ID and result.-k totalruns only tests whose names contain "total". It also accepts expressions such as-k "total and not tax".--maxfail=1stops after the first failure.-xis the shorthand.-m "not slow"selects tests by marker. Markers are covered in a later chapter.-qshortens the output.-sshowsprintoutput that pytest normally captures.--lfreruns only the tests that failed last time.
Exit codes
Pytest sets a process exit code that scripts and CI systems use. 0 means all tests passed. 1 means some tests failed. 2 means the run was interrupted, and 5 means no tests were collected.
Common mistakes
- Running pytest from the wrong directory, so imports like
from cart import totalfail. Run from the project root. - Naming a file
tests.pyor a functioncheck_total. Neither matches the discovery rules, so pytest skips it silently. - Reading only the last line. The
Elines above it explain why the test failed.
Exercise
You run pytest -k tax --maxfail=1 and the output ends with "1 failed, 1 passed" and then stops, even though the project has ten tests. Why did only two tests run?
Show answer
-k tax selected only tests whose names contain "tax". --maxfail=1 stopped the session after the first failure. The other tests were deselected or never reached.