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.
- pytest-rerunfailures
re-executes failed tests and is particularly helpful in the case of faulty tests.
- pytest-repeat
makes it easy to repeat one or more tests.
- pytest-order
enables the order to be defined using Markers.
- pytest-randomly
runs the tests in random order, first by file, then by class, then by test file.
… 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.- 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.
- pytest-html
enables the creation of HTML reports. Reports can be extended with additional data and images, such as screenshots of error cases.
- pytest-icdiff
improves diffs in the error messages of the pytest assertion with ICDiff.
… web development¶
pytest is used extensively for testing web projects and there is a long list of plugins that further simplify testing:
- pytest-selenium
provides fixtures that enable simple configuration of browser-based tests with Selenium.
- pytest-splinter
provide the high-level API of the Selenium-based Splinter to be used more easily from pytest.
- pytest-httpx
… 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.
- pytest-factoryboy
contains fixtures for factory-boy, a database model data generator.
- pytest-mimesis
generates fake data similar to Faker, but Mimesis is a lot faster.
… various things¶
- pytest-cov
executes the Coverage during testing.
- pytest-benchmark
performs benchmark timing for code within tests.
- pytest-timeout
prevents tests from running too long.
- pytest-asyncio
tests asynchronous functions.
- pytest-mock
is a thin wrapper around the unittest.mock patching API.
- pytest-freezegun
freezes the time so that any code that reads the time, date or clock time will get the same value during a test. set a specific time.
- pytest-grpc
is a Pytest plugin for gRPC.
- pytest-bdd
writes BDD tests with pytest.
Own plugins¶
See also