Plugins

As powerful as pytest is, it can do even more when we add plugins. The pytest codebase is designed to allow customisation and extensions, and there are hooks that allow changes and improvements through plugins.

You may be surprised to find that you have already written some plugins if you have worked through the previous sections. Every time you add fixtures or hook functions to a project’s conftest.py file, you are creating a local plugin. It’s just a little extra work to turn these conftest.py files into installable plugins that you can share between projects, with other people, or with the world.

But first, let’s start with where you can find third-party plugins. There are quite a few plugins out there, so there’s a good chance that any changes you want to make to pytest have already been written.

Finding plugins

You can find third-party pytest plugins in various places, for example the pytest documentation contains an alphabetical list of plugins from pypi.org. You can also search pypi.org itself, for pytest or for the pytest framework. Finally, many popular pytest plugins can also be found in pytest-dev on GitHub.

Installing plugins

Like other Python packages, pytest plugins can be easily installed with pip: python -m pip install pytest-cov.

Plugins for …

… modified test sequences

pytest usually executes our tests in a predictable order. For a directory of test files, pytest executes each file in alphabetical order. Within each file, each test is executed in the order in which it appears in the file. However, it can sometimes be useful to change this order. The following plugins change the usual sequence of a test:

pytest-xdist

executes tests in parallel, either with several CPUs on one machine or several remote machines.

Stars Contributors Commit activity Lizenz
pytest-freethreaded

for checking whether tests and libraries are thread-safe with Python 3.13’s experimental freethreaded mode.

Stars Contributors Commit activity Lizenz
pytest-rerunfailures

re-executes failed tests and is particularly helpful in the case of faulty tests.

Stars Contributors Commit activity Lizenz
pytest-repeat

makes it easy to repeat one or more tests.

Stars Contributors Commit activity Lizenz
pytest-order

enables the order to be defined using Markers.

Stars Contributors Commit activity Lizenz
pytest-randomly

runs the tests in random order, first by file, then by class, then by test file.

Stars Contributors Commit activity Lizenz

… modified output

The normal pytest output mainly shows dots for passed tests and characters for other output. If you pass -v, you will see a list of test names with the result. However, there are plugins that change the output even further:

pytest-instafail

adds a --instafail option that reports tracebacks and output from failed tests immediately after the failure. Normally, pytest reports tracebacks and output from failed tests only after all tests have completed.

Stars Contributors Commit activity Lizenz
pytest-edit

opens an editor after a failed test.

Stars Contributors Commit activity Lizenz
pytest-sugar

shows green checkmarks instead of dots for passed tests and has a nice progress bar. Like pytest-instafail, it also shows failures immediately.

Stars Contributors Commit activity Lizenz
pytest-html

enables the creation of HTML reports. Reports can be extended with additional data and images, such as screenshots of error cases.

Stars Contributors Commit activity Lizenz
pytest-icdiff

improves diffs in the error messages of the pytest assertion with ICDiff.

Stars Contributors Commit activity Lizenz

… web development

pytest is used extensively for testing web projects and there is a long list of plugins that further simplify testing:

pytest-httpx

facilitates the testing of HTTPX and FastAPI applications.

Stars Contributors Commit activity Lizenz
Playwright for Python

was specially developed for end-to-end testing. Playwright supports all modern rendering engines such as Chromium, WebKit and Firefox with a single API.

Stars Contributors Commit activity Lizenz
pyleniumio

is a thin Python wrapper around Selenium with simple and clear syntax.

Stars Contributors Commit activity Lizenz
pytest-selenium

provides fixtures that enable simple configuration of browser-based tests with Selenium.

Stars Contributors Commit activity Lizenz

… fake data

We have already used Faker in Combining markers with fixtures to create multiple item instances. There are many cases in different areas where it is helpful to generate fake data. It is therefore not surprising that there are several plugins that fulfil this need:

Faker

generates fake data for you and offers a faker fixture for use with pytest.

Stars Contributors Commit activity Lizenz
pytest-factoryboy

contains fixtures for factory-boy, a database model data generator.

Stars Contributors Commit activity Lizenz

… various things

pytest-cov

executes the Coverage during testing.

Stars Contributors Commit activity Lizenz
pytest-benchmark

performs benchmark timing for code within tests.

Stars Contributors Commit activity Lizenz
pytest-timeout

prevents tests from running too long.

Stars Contributors Commit activity Lizenz
pytest-asyncio

tests asynchronous functions.

Stars Contributors Commit activity Lizenz
pytest-mock

is a thin wrapper around the unittest.mock patching API.

Stars Contributors Commit activity Lizenz
pytest-patterns

provides a pattern matching engine optimised for tests.

Stars Contributors Commit activity Lizenz
pytest-grpc

is a Pytest plugin for gRPC.

Stars Contributors Commit activity Lizenz
pytest-bdd

writes BDD tests with pytest.

Stars Contributors Commit activity Lizenz

Own plugins

See also