pytest插件_Pytest插件来爱❤️

本文介绍了pytest插件的使用,旨在提升Python测试的效率和便捷性。通过引用一篇来自Towards Data Science的文章,读者可以了解到如何利用pytest插件优化测试流程,特别是对于数据科学家和开发者来说,这些工具能够加强测试体验。
摘要由CSDN通过智能技术生成

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)

Image for post
Photo by Jeremy Thomas on Unsplash
杰里米·托马斯 ( Jeremy Thomas)摄影: Unsplash

The default output of pytest is already good, but some plugins make it amazing. pytest-sugar is one of those plugins❤

pytest的默认输出已经不错,但是一些插件使它变得惊人。 pytest-sugar是其中的一个插件❤

Image for post
Pytest sugar changes the dots to checkmarks and the percentage to a bar. Image by Martin Thoma.
Pytest糖将点更改为选中标记,将百分比更改为条形。 图片由Martin Thoma提供。

If those dots or checkmarks are too decent for you, givepytest-emoji and pytest-emoji-out a try 😃

如果这些点或复选标记对您来说太合适了,请pytest-emojipytest-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是我在研究本文时才发现的插件-一见钟情🥰❤

Image for post
Image by Martin Thoma
图片由Martin Thoma提供

Very similar is pytest-clarity — be aware, that pytest-clarity is only active when you execute pytest -vv:

pytest-clarity非常相似—请注意,只有在执行pytest -vv时pytest-clarity才处于活动状态:

Image for post
Screenshot by Martin Thoma
Martin Thoma的屏幕截图

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是您的朋友。 它生成这样的报告:

Image for post
Screenshot by Martin Thoma
Martin Thoma的屏幕截图

Now that we are happy with the output, we want to make it lightning fast!

现在我们对输出感到满意,我们想使其快速闪电化!

我们需要速度! (We need speed!)

Image for post
Photo by chuttersnap on Unsplash
chuttersnapUnsplash拍摄的照片

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 autoand your tests run in parallel!pytest-parallelmight 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超级快,但是分解只是需要更多的计算方式:

Image for post
Minimum, Mean and Maximum execution time, as well as the standard deviation and the interquartile range, give you some insights into the execution time distribution. Image by Martin Thoma
最小,平均和最大执行时间,以及标准偏差和四分位数范围,使您可以洞悉执行时间的分布。 图片由Martin Thoma提供

独特的 (The unique ones)

Image for post
Photo by Darius Cotoi on Unsplash
Darius CotoiUnsplash拍摄的照片

Some plugins are unique and don’t fit in any of the other categories:

一些插件是唯一的,不适合其他任何类别:

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.

以下插件只有在您使用为其编写应用程序的情况下才对您感兴趣。 他们通常提供固定装置/模型。

Web开发 (Web Development)

适用于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 configurations

    pytest-aws :测试AWS资源配置

  • pytest-lambda : Fixtures for AWS Lambda

    pytest-lambda :AWS Lambda的固定装置

  • pytest-localstack : Create AWS integration tests via a Localstack Docker container

    pytest-localstack :通过Localstack Docker容器创建AWS集成测试

我不确定的插件 (Plugins I’m uncertain about)

Image for post
Photo by bruce mars on Unsplash
布鲁斯· 玛斯 ( Bruce mars)Unsplash拍摄的照片

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 and pytest-docker-compose: I would just build the Docker image and execute the stuff in it.

    pytest-docker-toolspytest-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 , and pytest-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-specpytest-pspecpytest-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-sugarpytest-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插件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值