8 个很棒的 pytest 插件
pytest 测试 Python

> 
> Python 测试工具最好的一方面是其强大的生态系统。这里列出了八个最好的插件。
> 
> 
> 


![](/data/attachment/album/201811/19/114149ufkyyfiq3n4n3y5z.jpg)


我们是 [pytest](https://docs.pytest.org/en/latest/) 的忠实粉丝，并将其作为工作和开源项目的默认 Python 测试工具。在本月的 Python 专栏中，我们分享了为什么我们喜欢 `pytest` 以及一些让 `pytest` 测试工作更有趣的插件。


### 什么是 pytest？


正如该工具的网站所说，“pytest 框架可以轻松地编写小型测试，也能进行扩展以支持应用和库的复杂功能测试。”


`pytest` 允许你在任何名为 `test_*.py` 的文件中定义测试，并将其定义为以 `test_*` 开头的函数。然后，pytest 将在整个项目中查找所有测试，并在控制台中运行 `pytest` 时自动运行这些测试。pytest 接受[标志和参数](https://docs.pytest.org/en/latest/usage.html)，它们可以在测试运行器停止时更改，这些包含如何输出结果，运行哪些测试以及输出中包含哪些信息。它还包括一个 `set_trace()` 函数，它可以进入到你的测试中。它会暂停您的测试， 并允许你与变量进行交互，不然你只能在终端中“四处翻弄”来调试你的项目。


`pytest` 最好的一方面是其强大的插件生态系统。因为 `pytest` 是一个非常流行的测试库，所以多年来创建了许多插件来扩展、定制和增强其功能。这八个插件是我们的最爱。


### 8 个很棒的插件


#### 1、pytest-sugar


[pytest-sugar](https://github.com/Frozenball/pytest-sugar) 改变了 `pytest` 的默认外观，添加了一个进度条，并立即显示失败的测试。它不需要配置，只需 `pip install pytest-sugar`，用 `pytest` 运行测试，来享受更漂亮、更有用的输出。


#### 2、pytest-cov


[pytest-cov](https://github.com/pytest-dev/pytest-cov) 在 `pytest` 中增加了覆盖率支持，来显示哪些代码行已经测试过，哪些还没有。它还将包括项目的测试覆盖率。


#### 3、pytest-picked


[pytest-picked](https://github.com/anapaulagomes/pytest-picked) 对你已经修改但尚未提交 `git` 的代码运行测试。安装库并运行 `pytest --picked` 来仅测试自上次提交后已更改的文件。


#### 4、pytest-instafail


[pytest-instafail](https://github.com/pytest-dev/pytest-instafail) 修改 `pytest` 的默认行为来立即显示失败和错误，而不是等到 `pytest` 完成所有测试。


#### 5、pytest-tldr


一个全新的 `pytest` 插件，可以将输出限制为你需要的东西。`pytest-tldr`（`tldr` 代表 “too long, didn’t read” —— 太长，不想读），就像 pytest-sugar 一样，除基本安装外不需要配置。不像 pytest 的默认输出那么详细，[pytest-tldr](https://github.com/freakboy3742/pytest-tldr) 将默认输出限制为失败测试的回溯信息，并忽略了一些令人讨厌的颜色编码。添加 `-v` 标志会为喜欢它的人返回更详细的输出。


#### 6、pytest-xdist


[pytest-xdist](https://github.com/pytest-dev/pytest-xdist) 允许你通过 `-n` 标志并行运行多个测试：例如，`pytest -n 2` 将在两个 CPU 上运行你的测试。这可以显著加快你的测试速度。它还包括 `--looponfail` 标志，它将自动重新运行你的失败测试。


#### 7、pytest-django


[pytest-django](https://pytest-django.readthedocs.io/en/latest/) 为 Django 应用和项目添加了 `pytest` 支持。具体来说，`pytest-django` 引入了使用 pytest fixture 测试 Django 项目的能力，而省略了导入 `unittest` 和复制/粘贴其他样板测试代码的需要，并且比标准的 Django 测试套件运行得更快。


#### 8、django-test-plus


[django-test-plus](https://django-test-plus.readthedocs.io/en/latest/) 并不是专门为 `pytest` 开发，但它现在支持 `pytest`。它包含自己的 `TestCase` 类，你的测试可以继承该类，并使你能够使用较少的按键来输出频繁的测试案例，例如检查特定的 HTTP 错误代码。


我们上面提到的库绝不是你扩展 `pytest` 的唯一选择。有用的 pytest 插件的前景是广阔的。查看 [pytest 插件兼容性](https://plugincompat.herokuapp.com/)页面来自行探索。你最喜欢哪些插件？




---


via: <https://opensource.com/article/18/6/pytest-plugins>


作者：[Jeff Triplett](https://opensource.com/users/jefftriplett), [Lacery Williams Henschel](https://opensource.com/users/laceynwilliams) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
We are big fans of [pytest](https://docs.pytest.org/en/latest/) 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](https://docs.pytest.org/en/latest/usage.html) 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](https://plugincompat.herokuapp.com/) page to explore on your own. Which ones are your favorites?

## Comments are closed.