8 great pytest plugins

One of the best aspects of the popular Python testing tool is its robust ecosystem. Here are eight of the best pytest plugins available.
293 readers like this.
Programming keyboard.

Opensource.com

We are big fans of pytest and use it as our default Python testing tool for work and open source projects. For this month's Python column, we're sharing why we love pytest and some of the plugins that make testing with pytest so much fun.

What is pytest?

As the tool's website says, "The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries."

Pytest allows you to define your tests in any file called test_*.py and as functions that begin with test_*. Pytest will then find all your tests, across your whole project, and run them automatically when you run pytest in your console. Pytest accepts flags and arguments that can change when the testrunner stops, how it outputs results, which tests are run, and what information is included in the output. It also includes a set_trace() function that can be entered into your test; this will pause your tests and allow you to interact with your variables and otherwise "poke around" in the console to debug your project.

One of the best aspects of pytest is its robust plugin ecosystem. Because pytest is such a popular testing library, over the years many plugins have been created to extend, customize, and enhance its capabilities. These eight plugins are among our favorites.

Great 8

1. pytest-sugar

pytest-sugar changes the default look and feel of pytest, adds a progress bar, and shows failing tests instantly. It requires no configuration; just pip install pytest-sugar, run your tests with pytest, and enjoy the prettier, more useful output.

2. pytest-cov

pytest-cov adds coverage support for pytest to show which lines of code have been tested and which have not. It will also include the percentage of test coverage for your project.

3. pytest-picked

pytest-picked runs tests based on code that you have modified but not committed to git yet. Install the library and run your tests with pytest --picked to test only files that have been changed since your last commit.

4. pytest-instafail

pytest-instafail modifies pytest's default behavior to show failures and errors immediately instead of waiting until pytest has finished running every test.

5. pytest-tldr

A brand-new pytest plugin that limits the output to just the things you need. pytest-tldr (the tldr stands for "too long, didn't read"), like pytest-sugar, requires no configuration other than basic installation. Instead of pytest's default output, which is pretty verbose, pytest-tldr's default limits the output to only tracebacks for failing tests and omits the color-coding that some find annoying. Adding a -v flag returns the more verbose output for those who prefer it.

6. pytest-xdist

pytest-xdist allows you to run multiple tests in parallel via the -n flag: pytest -n 2, for example, would run your tests on two CPUs. This can significantly speed up your tests. It also includes the --looponfail flag, which will automatically re-run your failing tests.

7. pytest-django

pytest-django adds pytest support to Django applications and projects. Specifically, pytest-django introduces the ability to test Django projects using pytest fixtures, omits the need to import unittest and copy/paste other boilerplate testing code, and runs faster than the standard Django test suite.

8. django-test-plus

django-test-plus isn't specific to pytest, but it now supports pytest. It includes its own TestCase class that your tests can inherit from and enables you to use fewer keystrokes to type out frequent test cases, like checking for specific HTTP error codes.


The libraries we mentioned above are by no means your only options for extending your pytest usage. The landscape for useful pytest plugins is vast. Check out the Pytest Plugins Compatibility page to explore on your own. Which ones are your favorites?

User profile image.
Lacey Williams Henschel is a software engineer with REVSYS and part of the organizing team for DjangoCon US. In the past, she's chaired DjangoCon US, organized several Django Girls workshops, taught courses for Treehouse, and written about accessibility at tech events.
User profile image.
Jeff Triplett is an open source developer and works as a software engineer, consultant, and partner at REVSYS. He is a Board of Director at The Python Software Foundation. He occasionally blogs on his website.

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.