pytest / Chapter 4 of 4

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.

2 topicsUpdated
25 / 26

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.

PythonCapture warning records and cache data
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) == 42

pytest.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.

PythonTest a plugin's own behavior
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)
26 / 26

Quick lookup and further reading#

Use the language here to decode pytest output and choose the right mechanism.

Reference documentation: pytest, fixtures, parametrization, monkeypatch, and pytest-asyncio. Check plugin options against the installed release.