pytest插件
Pytest is extensible and has plenty of plugins. You don’t need to use any of them, but you might find some very useful. I love this because you have an easy time to get started with unit testing, while still finding amazing stuff when you’re more experienced 🤩
Pytest是可扩展的,并且具有许多插件。 您不需要使用它们中的任何一个,但是您可能会发现它们非常有用。 我之所以喜欢它,是因为您有一个轻松的时间开始进行单元测试 ,而当您更有经验时仍然可以找到很棒的东西🤩
In this article, I’ll show you examples of plugins I use and the plugins I found while writing this article. You might want to cover the basics of unit testing first or refresh testing details like fixtures.
在本文中,我将向您展示我使用的插件示例以及在撰写本文时发现的插件。 您可能需要先介绍单元测试的基础知识,或刷新诸如Fixtures之类的测试详细信息 。
如何添加插件? (How can I add a plugin?)
All plugins presented in this article can be installed via pip
. Most of them are then already active. For example, when you install pytest-sugar via
本文介绍的所有插件都可以通过pip
安装。 然后,其中大多数已经处于活动状态。 例如,当您通过以下方式安装pytest-sugar时
pip install pytest-sugar
You can just execute pytest
and the plugin will automatically work. Others need to be used more directly. For example, after installing pytest-timeout
you need to specify the timeout parameter you want to use:
您只需执行pytest
,插件就会自动运行。 其他的则需要更直接地使用。 例如,在安装pytest-timeout
您需要指定要使用的timeout参数:
pytest --timeout=1
Don’t worry, I will explain those two plugins later 🙂
不用担心,我稍后会解释这两个插件🙂
有多少个Pytest插件? (How many Pytest plugins exist?)
Searching on pypi.org for the trove classifier Framework :: Pytest
, I found 668 packages. A stunning 1053 packages have “pytest” in the name. 461 packages have both, the name and the trove classifier.
在pypi.org上搜索trove分类器Framework :: Pytest
,我发现了668个软件包。 令人惊叹的1053软件包名称中带有“ pytest”。 461个程序包同时包含名称和旅行分类器。
I went through over 700 pytest-plugins for this article. I’ve jumped over plugins that consider themselves to be in planning, pre-alpha, or alpha stage. I’ve also skipped packages where I’ve seen a TODO
in the readme or if the package had less than 10 stars on GitHub.
本文涉及700多个pytest插件。 我跳过了认为自己处于计划,预Alpha或Alpha阶段的插件。 我还跳过了在自述文件中看到TODO
软件包,或者如果该软件包在GitHub上的星标不足10个,则会跳过这些软件包。
The packages I’ve found make pytest sparkle ✨, improve the speed 🏎, are specialized to specific packages, or just unique in their behavior ❄️
我发现的软件包会使pytest闪烁spark,提高速度🏎,专门针对特定的软件包,或者只是行为独特❄️
Last but not least, there are some plugins where I’m not sure if they are awesome or if they are a bad idea. Let’s jump right into it and have a look yourself!
最后但并非最不重要的一点是,在某些插件中,我不确定它们是否很棒或不是一个好主意。 让我们跳进去看看自己吧!
Side note: Maybe you remember my side note on typo squatting? I found a fixable issue on PyPI while writing this article, hopefully improving security for the community🎉
旁注 :也许您还记得我关于错字蹲的旁注 ? 在撰写本文时,我在PyPI上发现了一个可修复的问题 ,希望能改善社区的安全性🎉
闪亮的 (The shiny ones)
The default output of pytest is already good, but some plugins make it amazing. pytest-sugar
is one of those plugins❤
pytest的默认输出已经不错,但是一些插件使它变得惊人。 pytest-sugar
是其中的一个插件❤

If those dots or checkmarks are too decent for you, givepytest-emoji
and pytest-emoji-out
a try 😃
如果这些点或复选标记对您来说太合适了,请pytest-emoji
和pytest-emoji-out
out😃
The summary output now looks good, but the diffs between the expected value and the actual value can be improved. pytest-icdiff
is a plugin I’ve only found while researching this article — and it was love at first sight 🥰❤
现在,摘要输出看起来不错,但是可以改善期望值和实际值之间的差异。 pytest-icdiff
是我在研究本文时才发现的插件-一见钟情🥰❤

Very similar is pytest-clarity
— be aware, that pytest-clarity is only active when you execute pytest -vv
:
pytest-clarity
非常相似—请注意,只有在执行pytest -vv
时pytest-clarity才处于活动状态:

Once you're happy with the terminal output, you might think about getting reports in the browser. This could help once you have to have a look at many things, want to scroll and search. Then pytest-html
is your friend. It generates reports like this one:
对终端输出感到满意后,您可能会考虑在浏览器中获取报告。 一旦您不得不看许多东西,想要滚动和搜索,这可能会有所帮助。 然后pytest-html
是您的朋友。 它生成这样的报告:

Now that we are happy with the output, we want to make it lightning fast!
现在我们对输出感到满意,我们想使其快速闪电化!
我们需要速度! (We need speed!)
Plugins can speed things up. For example, you can make pytest fail instantly with pytest-instafail
instead of executing all remaining tests. For tests which might take a long time or even result in an infinite loop in case of errors, I use pytest-timeout
❤. That is especially helpful when you apply Mutation testing.
插件可以加快速度。 例如,您可以使用pytest-instafail
使pytest立即失败,而不是执行所有剩余测试。 对于可能花费很长时间甚至在出现错误的情况下导致无限循环的测试,我使用pytest-timeout
。 当您应用突变测试时,这特别有用。
We also want to use our machine properly by using pytest-xdist
. Install it, execute pytest -n auto
and your tests run in parallel!pytest-parallel
might also be worth a shot.
我们还想通过使用pytest-xdist
来正确使用我们的机器。 安装它,执行pytest -n auto
,您的测试将并行运行! pytest-parallel
也可能值得一试。
The most extreme speedup is not to execute stuff you don’t need. pytest-picked
executes tests that are related to unstaged files which can be way less than your complete test suite.
最极端的加速是不执行不需要的东西。 pytest-picked
执行与未暂存文件相关的测试,这些文件可能比完整的测试套件要少。
Going in a different direction, we want to make sure that the algorithms have some speed behavior. With pytest-benchmark
, we can use the benchmark
fixture to annotate parts of a test which we want to benchmark:
朝另一个方向发展,我们要确保算法具有一定的速度行为。 通过pytest-benchmark
,我们可以使用benchmark
夹具来注释要基准测试的部分:
def test_argmax(benchmark):
assert benchmark(mpu.math.argmax, [1, 2, 3]) == 2
Running pytest
then also gives this output, where you can see three functions to which I’ve added a benchmark. Two of them test a factorization function. It should not be a surprise that factorizing 3072 takes longer than factorizing 1024, but it is always astonishing to me how quickly the numbers grow. The argmax of 3 examples is super quick, but factorization just needs way more computation:
然后运行pytest
也会给出此输出,您可以在其中看到我添加了基准的三个函数。 其中两个测试分解因子函数。 分解3072所需的时间比分解1024所需的时间长,这不足为奇,但我总是惊讶于数字的增长速度。 3个示例的argmax超级快,但是分解只是需要更多的计算方式:

独特的 (The unique ones)
Some plugins are unique and don’t fit in any of the other categories:
一些插件是唯一的,不适合其他任何类别:
pytest-cov
: Get a test coverage report 😍 ❤ I like to generate both, an HTML report and an output to the terminal. In some settings, an XML report is also helpful.pytest-cov
:获取测试覆盖率报告❤❤我想同时生成HTML报告和终端输出。 在某些设置中,XML报告也很有帮助。pytest-socket
: Make certain that you don’t query anything non-local. Very nice ❤pytest-socket
:确保您不查询任何非本地的内容。 很好❤pytest-randomly
andpytest-rng
: If you userandom.random
, then the outcome depends on the random seed. This plugin changes the seed.pytest-randomly
和pytest-rng
:如果使用random.random
,则结果取决于随机种子。 这个插件改变了种子。pytest-random-order
: Execute the tests in a random order, to make sure you see when a test leaves the system in a different state.pytest-random-order
:以随机顺序执行测试,以确保您看到测试何时将系统置于其他状态。pytest-lazy-fixtures
: Use fixtures in@pytest.mark.parametrize
.pytest-lazy-fixtures
:在@pytest.mark.parametrize
使用固定装置。pytest-freezegun
: Freeze time! This is one I’ve also mentioned in my patching article.pytest-freezegun
:冻结时间! 我在修补文章中也提到了这一点。pytest-leaks
: Find resource leaks. This requires a debug-built of Python!pytest-leaks
:查找资源泄漏。 这需要使用Python调试构建!pytest-flake8
: Run flake8 via pytest. I did that for a long time, but when I learned how to use Continuous Integration pipelines more effectively, I stopped it. You can still execute flake8 directly.pytest-flake8
:通过pytest运行flake8。 我做了很长时间,但是当我学会了如何更有效地使用持续集成管道时 ,我停止了。 您仍然可以直接执行flake8。pytest-mypy
andpytest-mccabe
: Same story as for flake8. By the way, type annotations are awesome! I like to have those static code analysis tools in a linter step within the CI pipeline.pytest-mypy
和pytest-mccabe
:与pytest-mccabe
故事相同。 顺便说一句, 类型注释很棒 ! 我喜欢在CI管道的后续步骤中使用这些静态代码分析工具 。pytest-deadfixtures
: Point out which fixtures are not used or duplicated.pytest-deadfixtures
:指出未使用或重复的灯具。
17个专用插件—您会知道是否需要它们 (17 Specialized Plugins — You’ll know if you need them)
The following plugins are only interesting to you if you work with the applications for which they are written. They usually provide fixtures/mocks.
以下插件只有在您使用为其编写应用程序的情况下才对您感兴趣。 他们通常提供固定装置/模型。
pytest-cookie
: Supports testing of cookiecutter templates.pytest-cookie
:支持的测试18.11模板。pytest-plt
andpytest-mpl
: Provides fixtures for matplotlib.pytest-plt
和pytest-mpl
:为matplotlib提供固定装置。pytest-responses
: Provides fixtures forrequests
.pytest-responses
:为requests
提供固定装置。pytest-asyncio
: Use it when you develop async functions.pytest-asyncio
:开发异步功能时使用它。pytest-qt
: GUI development via Qt / PySide / PySide2.pytest-qt
:通过Qt / PySide / PySide2开发GUI。
Web开发 (Web Development)
pytest-djangoapp
/pytest-djangoqueries
: Exposes tools for Django application developers to facilitate test authoring, including settings override, template tag testing, and user creation.pytest-djangoapp
/pytest-djangoqueries
:为Django应用程序开发人员公开工具,以方便测试创作,包括设置覆盖,模板标签测试和用户创建。pytest-flask
andpytest-flask-sqlalchemy
: Provides fixtures for running tests in transactions using Flask-SQLAlchemy.pytest-flask
和pytest-flask-sqlalchemy
:提供用于在使用Flask-SQLAlchemy的事务中运行测试的装置。
适用于AWS的模拟和固定装置 (Mocks and Fixtures for AWS)
moto
: Mocks for boto3 — AWS stuff. I don’t exactly love this one, but it is for sure the best you can do when you want to test code that uses S3.moto
:boto3的模拟-AWS内容。 我并不完全喜欢这个,但是可以肯定的是,当您要测试使用S3的代码时,可以做的最好。pytest-aws
: Testing AWS resource configurationspytest-aws
:测试AWS资源配置pytest-lambda
: Fixtures for AWS Lambdapytest-lambda
:AWS Lambda的固定装置pytest-localstack
: Create AWS integration tests via a Localstack Docker containerpytest-localstack
:通过Localstack Docker容器创建AWS集成测试
我不确定的插件 (Plugins I’m uncertain about)
The following plugins sounded cool for me when I first read about them, but for various reasons, I’m uncertain if they are really a good idea:
当我初次阅读以下插件时,这些插件对我来说听起来很酷,但是由于种种原因,我不确定它们是否真的是个好主意:
pytest-check
: Allows multiple failures per test. At first, I loved the idea. Then I realized that this might lead to worse tests as the tests start to do many things. On the other hand, you might want to test a “workflow” once — so not a unit test, but an integration test or even an end-to-end test. But then you would also need intermediate results.pytest-check
:每次测试允许多次失败。 起初,我喜欢这个主意。 然后我意识到,随着测试开始做很多事情,这可能会导致更糟糕的测试。 另一方面,您可能要测试一次“工作流”,所以不是单元测试,而是集成测试,甚至是端到端测试。 但是,那么您还需要中间结果。pytest-docker-tools
andpytest-docker-compose
: I would just build the Docker image and execute the stuff in it.pytest-docker-tools
和pytest-docker-compose
:我只是构建Docker映像并执行其中的内容。pytest-mock
: Provides a mocker fixture which is a thin wrapper around the patching API provided by the mock package.pytest-mock
:提供一个pytest-mock
夹具,它是模拟套件提供的修补API的薄包装。pytest-spec
,pytest-pspec
, andpytest-testdox
modify the pytest output. They show what is tested. The tests should be written in such a way that they represent the software specification — so the test is against a part of the specification.pytest-spec
,pytest-pspec
和pytest-testdox
修改pytest输出。 它们显示了经过测试的内容。 测试应该以代表软件规范的方式编写—因此测试违反了规范的一部分。pytest-recording
: It should record network interactions via VCR.py, but I didn’t get it to work.pytest-recording
:它应该通过VCR.py记录网络交互,但是我没有使其工作。pytest-dependency
allows you to specify which tests need to succeed for others to be able to succeed. Unit tests should be independent and dependent code should be mocked…maybe. I’m not certain about that.pytest-dependency
允许您指定哪些测试需要成功才能使其他人成功。 单元测试应该是独立的,并且应该模拟依赖的代码……也许。 我不确定。
TL; DR (TL;DR)
pytest
is the tool of choice to run tests in Python. While it has reasonable defaults, it’s extensive plugin system lets you customize it to make it even better.
pytest
是在Python中运行测试的首选工具。 尽管它具有合理的默认值,但它的扩展插件系统使您可以对其进行自定义以使其更好。
I love pytest-sugar
and pytest-icdiff
, because they make the output of pytest easier to read. pytest-cov
generates line- and branch coverage and thus is a valuable tool to find spots that need better tests. The next step is to run the tests. You really don’t want to accidentally hit the production environment. This is wherepytest-socket
comes into play. It just blocks everything and reports it to you. The other type of issue are long-running tests that are potentially in infinite loops.pytest-timeout
kills those tests after the specified amount of time.
我喜欢pytest-sugar
和pytest-icdiff
,因为它们使pytest的输出更易于阅读。 pytest-cov
生成行覆盖和分支覆盖,因此是查找需要更好测试的点的有价值的工具。 下一步是运行测试。 您真的不想意外地破坏生产环境。 这就是pytest-socket
发挥作用的地方。 它只会阻止所有内容并将其报告给您。 另一类问题是可能长期处于无限循环中的长时间运行的测试。 pytest-timeout
在指定的时间后杀死那些测试。
There are so many other plugins; many add fixtures for specific packages which are typically hard to test. You should now have a good idea of the many possibilities added by pytest plugins — use them!
还有很多其他的插件。 许多添加了通常难以测试的特定包装的夹具。 现在,您应该对pytest插件添加的许多功能有了个好主意-使用它们!
翻译自: https://towardsdatascience.com/pytest-plugins-to-love-%EF%B8%8F-9c71635fbe22
pytest插件