Code Reference

Glossary

Pytest · Reference cheat sheet

Glossary

Pytest · Reference cheat sheet


📋 Overview

Alphabetical glossary of core Pytest terms for fixtures, assertions, markers, configuration, and test organization.

🔧 Core concepts

TermDefinition
Approxpytest.approx for comparing floating-point numbers with tolerance.
Assert rewritingPytest’s enhancement of assert statements with rich failure introspection.
CaplogA fixture that captures logging output during a test.
CapsysA fixture that captures writes to stdout and stderr.
CollectionThe phase where Pytest discovers and builds the test suite tree.
ConftestA conftest.py file that shares fixtures and hooks with a directory tree.
CoverageMeasurement of which code lines were executed by tests.
FixtureA reusable setup/teardown provider injected into tests by name.
HookA plugin callback such as pytest_runtest_setup that extends Pytest.
MarkerA tag like @pytest.mark.slow used to select or skip tests.
MonkeypatchA fixture for temporarily changing attributes, env vars, or dict items.
Node idThe unique path identifying a collected test, used in selection.
ParametrizeRunning one test function multiple times with different argument sets.
PluginAn extension package or module that adds fixtures, hooks, or CLI options.
Pytest.iniA common configuration file for options, markers, and paths.
RaiseUsing pytest.raises to assert that code throws an expected exception.
RequestThe fixture that gives access to the current test context and node.
ScopeHow long a fixture lives: function, class, module, package, or session.
SkipMarking a test to not run, optionally with a reason.
TeardownCleanup code that runs after a test or fixture yields.
Tmp_pathA fixture providing a unique temporary pathlib.Path per test.
TracebackThe failure stack Pytest prints, often shortened for readability.
UsefixturesA marker that applies fixtures to a test or class without parameters.
XdistThe pytest-xdist plugin that runs tests in parallel workers.
XfailExpecting a test to fail; unexpected passes are reported specially.

💡 Examples

Fixture and assert:

import pytest

@pytest.fixture
def user():
    return {"name": "Ada"}

def test_user_name(user):
    assert user["name"] == "Ada"

Parametrize and markers:

@pytest.mark.parametrize("n,expected", [(1, 2), (2, 4)])
def test_double(n, expected):
    assert n * 2 == expected

@pytest.mark.slow
def test_integration():
    ...

CLI selection:

pytest -k "user and not slow" -q
pytest -m "not slow" --maxfail=1

⚠️ Pitfalls

  • Confusing skip (do not run) with xfail (run, expect failure).
  • Mixing fixture scope (function vs session) and accidentally sharing mutable state.
  • Treating conftest.py like a normal importable module — discovery is directory-based.
  • Equating parametrize ids with test names — node ids include parameters.
  • Assuming plain assert elsewhere behaves like Pytest’s rewritten asserts.

On this page