Built-ins and API reference
Look up pytest built-in fixtures, helpers, testing terms, and reference links. Find tools for cache, assertions, and test-suite maintenance.
Specialized built-ins#
These tools cover less common tests without pulling in a plugin.
pytest.record_property adds metadata to a test's JUnit XML; pytest.recwarn captures warnings; pytest.cache stores small values between runs through request.config.cache. pytester is useful when you are testing a pytest plugin itself. See pytest --fixtures for the exact built-ins supported by your installed version.
import warnings
def test_parser_warning(recwarn):
warnings.warn("slow parser", UserWarning)
assert len(recwarn) == 1
assert "slow parser" in str(recwarn[0].message)
def test_cache_roundtrip(request):
cache = request.config.cache
cache.set("project/last-id", 42)
assert cache.get("project/last-id", None) == 42pytest.assume is not built in. For multiple assertions that should be reported independently, parameterize separate cases, or use supported subtests (pytest-subtests plugin or the subtests fixture in newer pytest versions). A pytester fixture typically needs pytest_plugins = ["pytester"] in your plugin test module or -p pytester.
pytest_plugins = ["pytester"]
def test_plugin_adds_option(pytester):
pytester.makepyfile("def test_ok(): assert True")
result = pytester.runpytest("-q")
result.assert_outcomes(passed=1)Quick lookup and further reading#
Use the language here to decode pytest output and choose the right mechanism.
- Test / case: A collected function invocation. Parametrization creates multiple cases from one function.
- Fixture: Named setup requested by a test or another fixture. Teardown runs after its scope ends.
- Scope: How long a fixture instance is shared: function, class, module, package, or session.
- Marker: Metadata on a test, used for selection or behavior such as skip and xfail.
- Collection: Discovering and importing tests before executing them.
- Node ID: Stable path-like test identifier, including a parameter case ID when applicable.
- Hook: Extension point invoked by pytest, used by conftest files and plugins.
- Plugin: Installed or explicitly loaded extension adding hooks, fixtures or CLI flags.
- Assertion rewriting: pytest's import-time transformation that explains failed
assertexpressions. - Capture: Saving stdout, stderr and logs to display with a failed test.
- Isolation: Ensuring one test's files, global state, database records or external calls do not affect another.
Reference documentation: pytest, fixtures, parametrization, monkeypatch, and pytest-asyncio. Check plugin options against the installed release.
Help improve CodeRef
Allow Google Analytics to measure page visits and use of the guides? It uses cookies after you accept. Your choice won’t affect access to the site. Privacy notice