We're sunsetting PodQuest on 2025-07-28. Thank you for your support!
Export Podcast Subscriptions
cover of episode pytest plugins - a full season

pytest plugins - a full season

2025/1/10
logo of podcast Test & Code

Test & Code

AI Deep Dive AI Insights AI Chapters Transcript
People
B
Brian Okken
Topics
Brian Okken: 我主要开始使用 Pytest 是因为它强大的 fixture 系统。和其他测试系统中的 setup 和 teardown 类似,但 Pytest 的 fixture 更加模块化、灵活,可以在整个测试套件中使用,并且可以通过插件系统与其他项目共享。 Pytest 的插件系统允许插件共享 fixture 和 hook 函数。hook 函数是修改 Pytest 行为的一种方式,例如添加命令行标志或根据运行时因素取消选择或跳过测试。 PyPI 上有大量的 Pytest 插件,数量至少有 1487 个,这得益于其强大的插件系统。pytest.org 上有一个插件列表,列出了这些插件,它们都是通过搜索 PyPI 上以 `pytest-` 开头的项目找到的。PyPI 上有超过 10000 个与 pytest 相关的项目,这有点多,因此我整理了一个更精简的列表,在 pythontest.com 上,名为 Top PyTest Plugins,它基于过去 30 天的下载次数排名,目前包含 133 个插件。 我使用了 Hugo VK 网站上的 Top PyPI Packages 数据集,该数据集每月更新一次,包含 PyPI 上下载次数最多的 8000 个软件包。我从中筛选出了 133 个 pytest 插件,并排除了不再受支持的软件包和 pytest 本身。这个列表会随着时间的推移而变化。 在这个系列中,我将介绍一些流行的 pytest 插件,并邀请听众分享他们喜欢的插件。我也邀请 pytest 插件的作者参与节目,讨论他们的插件。我将根据插件的流行度和个人兴趣选择介绍的插件,并非严格按照下载次数排名。 排名第一的插件是 pytest-cov,用于测量代码覆盖率,它使用 Coverage.py 并提供一些便利功能。其他一些插件,例如 pytest-asyncio、pytest-metadata、pytest-html、pytest-timeout、pytest-ordering、pytest-repeat、pytest-check 和 pytest-playwright,我也会进行介绍。pytest-check 是我自己的插件,它允许每个测试用例出现多个错误。pytest-md-report 插件可以生成 markdown 格式的测试结果报告。 我从 Top PyPI Packages 获取插件信息,包括名称、链接和下载次数,并从 PyPI API 获取插件描述。我将介绍排名前九的插件,包括 pytest-cov、pytest-mock、pytest-xdist、pytest-asyncio、pytest-metadata、pytest-html、pytest-timeout、pytest-rerunfailures 和 pytest-django,因为我正在开发一个 Django 项目。 这个系列的目的是让大家了解 pytest 插件,推广我的 Top PyTest Plugins 列表,并分享一些插件的使用经验。一些章节可能会比较短,因为有些插件我不太了解。我可能会将功能相似的插件组合在一起介绍,例如不同的排序插件。即使不使用某些插件,也可以学习它们在 fixture 或 hook 函数方面的用法。 pytest 的文档在 hook 函数的使用方面可能还不够完善,编写 pytest 插件的教程或书籍将很有帮助。我计划在这个系列中讨论插件的源代码和 hook 函数的使用。

Deep Dive

Key Insights

What makes PyTest fixtures unique compared to other test systems?

PyTest fixtures are more modular, flexible, and reusable across a test suite. They can also be shared with other projects through the plugin system, making them highly versatile.

What is the purpose of hook functions in PyTest plugins?

Hook functions allow plugins to modify PyTest's behavior, such as adding command-line flags or deselecting/skipping tests based on runtime factors.

How many PyTest plugins are listed on PyPI?

There are at least 1,487 PyTest plugins listed on PyPI, identified by searching for projects that begin with 'pytest-' or 'pytest_'.

What is the Top PyTest Plugins list on pythontest.com based on?

The Top PyTest Plugins list is based on the number of downloads over the past 30 days, currently featuring 133 plugins.

What is the most downloaded PyTest plugin?

The most downloaded PyTest plugin is PyTestCov, which measures test coverage using Coverage.py and provides additional conveniences.

What is PyTest Metadata used for?

PyTest Metadata is used to add metadata to test reports, often in conjunction with PyTest HTML, and is one of the most popular plugins.

What is PyTest Playwright, and why is it notable?

PyTest Playwright is a plugin for testing web applications using Playwright. It is notable for its utility in web projects and is on the speaker's list of plugins to explore.

What is PyTest MD Report, and how is it used?

PyTest MD Report generates test outcome reports in Markdown table format, making it useful for piping output to websites or other readable formats.

What are some of the top PyTest plugins mentioned in the episode?

The top PyTest plugins include PyTestCov, PyTestMock, XDist, AsyncIO, Metadata, HTML, Timeout, RerunFailures, and PyTest Django.

Why is learning from plugin source code valuable?

Learning from plugin source code provides insights into advanced uses of fixtures and hook functions, which are often not fully covered in official documentation.

Shownotes Transcript

This episode kicks off a season of pytest plugins.

In this episode:

  • Introduction to pytest plugins
  • The pytest.org pytest plugin list
  • Finding pytest related packages on PyPI
  • The Top pytest plugins list on pythontest.com
  • Exploring popular plugins
  • Learning from plugin examples

Links:

** Learn pytest**