pytest finds tests by walking the directories you give it and collecting files named test_*.py or *_test.py, then functions named test_* and methods named test_* inside classes named Test*. For failures, pytest rewrites the assert statements in test modules at import time, so a plain assert a == b reports both values and the difference. This is the pytest test discovery and assert behaviour you rely on every day.
Understanding both mechanisms explains most beginner surprises: why a test is not run, why imports fail, and why failure messages are so detailed. This guide uses pytest 8 on Python 3.13.
When you run pytest with no arguments, it starts at the current directory (or the testpaths setting, if configured) and applies these default rules:
norecursedirs such as .git, node_modules, .venv, build and dist.test_*.py or *_test.py are imported.test_*, and methods named test_* in classes whose names start with Test and which have no __init__ method.You can see exactly what was collected without running anything:
Each line is a node ID, in the form path::function or path::Class::method. You can pass a node ID to pytest to run just that test.
Keep tests in a tests directory next to your code and tell pytest where to look in pyproject.toml:
testpaths limits discovery to tests, pythonpath adds src to the import path so import shop works without installing, and -ra prints a short summary of skipped and failed tests. Installing your package in editable mode with python -m pip install -e . is the other common way to make imports work.
If your project uses other names, override the patterns in the same configuration section:
Be careful with broad patterns: any helper whose name matches will be collected as a test and may fail.
| Symptom | Likely cause |
|---|---|
| File is ignored | Name does not match test_*.py or *_test.py |
| Function is ignored | Name does not start with test, or it is nested inside another function |
| Class is ignored | Name does not start with Test, or it defines __init__ |
ModuleNotFoundError | Source folder is not on sys.path; set pythonpath or install the package |
| Two files with the same name error | Test directories without __init__.py and duplicate base names |
For the last case, either give test files unique names or add __init__.py files so each module has a unique package path. Recent pytest versions also support --import-mode=importlib, which avoids the problem without touching sys.path.
Python's own assert only tells you that it failed. pytest installs an import hook that rewrites the abstract syntax tree of test modules, and of modules registered with pytest.register_assert_rewrite, before they run. The rewritten code keeps the intermediate values so it can print them.
pytest shows only the differing items first, then a full diff. The same detail appears for lists, sets, strings and dataclasses, so you do not need assertDictEqual or other special methods.
You can add a message after the comma, and it is printed in addition to the introspection:
Avoid asserting on a tuple such as assert (x, "message"); a non-empty tuple is always true and Python warns about it.
| Option | Effect |
|---|---|
-v | Shows one line per test, and fuller diffs for long values |
-vv | Shows complete diffs without truncation |
--tb=short | Shorter tracebacks |
--tb=line | One line per failure |
-l | Shows local variables in tracebacks |
-ra | Summary of all non-passing results at the end |
If you debug code that is itself hidden by the rewriting, run with --assert=plain to turn rewriting off and compare behaviour.
Check the file name against test_*.py or *_test.py, run pytest --collect-only, and look for a testpaths or python_files setting that narrows the search.
Only test modules, conftest.py files and plugins are rewritten by default. For a helper module that contains asserts, call pytest.register_assert_rewrite("helpers") before it is imported.
__init__.py in the tests folder?Not with a simple layout and unique file names. Add it, or use --import-mode=importlib, when different folders contain test files with the same name.
No. Python removes assert statements when run with -O, so use explicit checks and exceptions in application code and keep assert for tests.
pytest collects test_*.py and *_test.py files, test_* functions and Test* classes, and you can inspect the result with --collect-only. Configure paths and patterns in pyproject.toml. Assert rewriting turns a plain assert into a detailed report, so you can write simple tests and still get precise failure messages.