Labs ICT
โญ Pro Login

pytest Plugin Architecture

How pytest's plugin system works and how to extend it.

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 reporting
  • pytest-html - HTML test reports
  • pytest-xdist - Parallel test execution
  • pytest-mock - Enhanced mocking with mocker fixture
  • pytest-django - Django testing support
  • pytest-asyncio - Async test support

๐Ÿงช Quick Quiz

Which of these is NOT a pytest plugin?