pytest - simple powerful testing with Python
pytest [-q] [-v] [-x] [-k EXPR] [FILE[::TEST]]...
Options you will use
-v- One line per test with PASSED/FAILED.
-q- Quiet: dots only.
-x- Stop at the first failure.
-k EXPR- Only run tests whose name contains EXPR ("not EXPR" to exclude).
-s- Do not capture output: print() shows up live (--capture=no).
--maxfail=N- Stop after N failures.
-m MARKEXPR- Only run tests matching the marker expression.
--lf, --last-failed- Rerun only the tests that failed last time.
--tb=STYLE- Traceback style: auto, long, short, line, no.
-r CHARS- Short summary of the given outcomes: f failed, E error, s skipped, a all but passes.
--junitxml=PATH- Write a JUnit XML report (what CI dashboards read).
--cov=PATH- Measure coverage of PATH (pytest-cov).
Examples
$ pytestCollect test_*.py / _test.py under the current directory and run every test_ function.
$ pytest test_checks.py::test_timeout -vRun one test.
Gotchas
- Plain assert statements are rewritten so a failure shows the values: "assert 1 == 2" and "+ where 1 = exit_code_for('DOWN')".
- Fixtures are functions decorated with @pytest.fixture; a test asks for one by naming it as a parameter. Built in: tmp_path (a fresh directory per test), monkeypatch (setattr/setenv, undone after the test), capsys (captured output).
- Exit codes: 0 all passed, 1 some failed, 2 interrupted (e.g. a collection error), 5 no tests collected.
Try pytest in a real terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.