pytest Plugin Architecture
pytest's power comes from its plugin system. Almost everything in pytest is implemented as a plugin, including test collection, reporting, and fixture management. You can extend pytest by writing your own plugins.
How Plugins Work
Plugins hook into specific stages of the test lifecycle using hooks. Common hooks include:
# pytest hooks you can implement in a plugin
def pytest_configure(config):
"""Called after command line options have been parsed."""
pass
def pytest_collection_modifyitems(session, config, items):
"""Called after test collection is complete."""
pass
def pytest_runtest_call(item):
"""Called before each test runs."""
pass
def pytest_runtest_makereport(item, call):
"""Called after each test to create a report."""
pass
Discovering Installed Plugins
List all installed plugins:
pytest --plugins
This shows every plugin pytest has loaded, including built-in ones.
Disabling Plugins
You can disable specific plugins when running tests:
# Disable a specific plugin
pytest -p no:cacheprovider
# Disable multiple plugins
pytest -p no:cacheprovider -p no:html
Popular Plugins
pytest-cov- Code coverage reportingpytest-html- HTML test reportspytest-xdist- Parallel test executionpytest-mock- Enhanced mocking withmockerfixturepytest-django- Django testing supportpytest-asyncio- Async test support