-
Notifications
You must be signed in to change notification settings - Fork 6
PyTest
A fixture (a reusable test dependency that pytest injects into tests by name) defaults to using its Python function name. It can be overridden with @pytest.fixture(name="...").
The fixture scope (the lifetime of a fixture instance, controlling how often it is created and reused) defaults to function. It can be overridden with scope="session", "package", "module", or "class".
Autouse fixtures (fixtures that run automatically for every test without being requested by name) default to autouse=False. They can be enabled with autouse=True.
Fixture parameters (values that cause pytest to run the fixture multiple times with different inputs) default to params=None. They can be set with params=[...].
Fixture parameter IDs (the human‑readable names pytest assigns to parametrized test cases) default to the string representation of each parameter. They can be overridden with ids=[...] or ids=callable.
Fixture teardown (cleanup code that runs after the fixture’s scope ends) is not present by default. It appears only when using yield or request.addfinalizer.
A fixture instance is created once per its scope. For the default function scope, it runs once per test.
Fixture ordering (the sequence in which fixtures are set up) is determined by the dependency graph. Independent fixtures have no guaranteed order.
Fixture lookup (the process pytest uses to find a fixture by name) follows this order: test function → test module → parent conftest.py files → builtin fixtures → plugin fixtures.
A missing fixture triggers a FixtureLookupError (pytest’s error for unresolved fixture names). This can be avoided by skipping or xfail inside a fixture.
Builtin fixtures (fixtures provided automatically by pytest) include tmp_path, tmp_path_factory, monkeypatch, capsys, capfd, caplog, recwarn, pytestconfig, and request.
Fixture injection (pytest’s name‑based dependency injection system) uses the fixture name to match parameters. It can be overridden with aliasing or request.getfixturevalue.
Fixture teardown occurs at the end of the fixture’s scope. This can be changed by altering scope or using nested fixtures.
Test discovery (pytest’s process for finding tests) defaults to files named test_*.py or *_test.py, classes named Test*, and functions named test_*.
The test collection root (the directory pytest treats as the project root) is auto‑detected based on the first parent containing a config file or VCS directory.
Collection errors (failures during test discovery) cause the session to error. They can be suppressed with --continue-on-collection-errors.
Assertion rewriting (pytest’s mechanism for enhancing Python assert statements with introspection) is enabled for test modules by default.
Assertion introspection (pytest’s detailed explanation of why an assertion failed) is enabled by default. It can be disabled by running Python in optimized mode.
Test execution order (the sequence in which tests run) defaults to file order.
A failing test stops only that test. The session continues unless -x or --maxfail is used.
The traceback style (the formatting of error stack traces) defaults to auto.
Pytest converts PytestUnhandledCoroutineWarning and PytestReturnNotNoneWarning into errors. Other warnings pass through.
Output capturing (pytest’s interception of stdout and stderr) is enabled by default. It can be disabled with -s.
Logging capture (pytest’s interception of Python logging output) is enabled only for failures. CLI logging is disabled by default.
Skipping (pytest’s mechanism for marking tests as intentionally not run) occurs only when tests are marked with skip or skipif.
Xfail (pytest’s “expected failure” marker) defaults to non‑strict. Unexpected passes are XPASS.
Markers (labels attached to tests for selection or behavior changes) are accepted without validation. They can be made strict with --strict-markers.
Plugin autoloading (pytest’s automatic loading of installed plugins) is enabled by default.
Pytest does not modify environment variables except internally.
tmp_path creates a unique directory per test. tmp_path_factory creates session‑level directories.
Test reporting (pytest’s summary of test outcomes) prints failures, errors, and skips.
Exit codes follow pytest’s standard mapping: 0 success, 1 failures, 2 usage errors, 3 internal errors, 4 no tests collected.
Parametrization (pytest’s mechanism for generating multiple test cases from one test function) defaults to using repr‑based IDs.
Hooks (pytest’s plugin extension points) default to no‑op implementations.
Test selection (choosing which tests to run) defaults to running all discovered tests.
Import mode (how pytest imports test modules) defaults to importlib.
Pytest adds the project root to sys.path.
Pytest does not retry tests by default.
Pytest does not run tests in parallel by default.
Doctests do not run unless explicitly enabled.
Pytest stops the session on the first interrupt.
Internal caching (pytest’s storage of run metadata) writes to .pytest_cache.
Pytest does not report slow tests unless durations are enabled.
Fail‑fast is disabled by default.
Keyword filtering (selecting tests by substring match) is disabled by default.
Marker filtering (selecting tests by marker expression) is disabled by default.
Pytest does not rerun tests by default.
Pytest loads plugins, discovers tests, and begins execution.
Pytest runs teardown hooks, fixture teardowns, and prints a summary.
The default is an empty string.
.*, build, dist, CVS, _darcs, {arch}, *.egg, venv, .venv
classic
.pytest_cache
WARNING
'%(levelname)s:%(name)s:%(message)s'
'%Y-%m-%d %H:%M:%S'
No.
WARNING
'%(levelname)s:%(name)s:%(message)s'
'%Y-%m-%d %H:%M:%S'
WARNING
'%(levelname)s:%(name)s:%(message)s'
'%Y-%m-%d %H:%M:%S'
test_*.py and *_test.py
Test*
test_*
No.
It errors on PytestUnhandledCoroutineWarning and PytestReturnNotNoneWarning.
No.
No.
0
utf-8
*.txt
No.
No.
auto
None.
0.005 seconds.
No.
0
module
pytest
no
No.
total
xunit2
No.
0
term
No.
No.
0
No.
No.