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

Translations:
中文

I started using PyTest primarily for the fixture system. PyTest fixtures are kind of like setup and teardown in other test systems, but way more modular and flexible and usable across a test suite, and you can share them with other projects through the plugin system. Welcome to Test & Code. This episode is brought to you by HelloPytest, the new fastest way to learn PyTest, and by the Python Test community. Find out more at courses.pythontest.com.

The PyTest plugin system allows plugins to share fixtures, but also allows them to share hook functions. So what's a hook function? A hook function is a way to modify part of the PyTest behavior. A couple of common examples are, for instance, adding a command line flag to be able to pass in information to the test run. You do that with a hook function.

Another example would be to deselect or skip some tests based on some runtime factor. You would do that in a hook function as well. This plugin system that allows sharing of fixtures and hook functions is so powerful and popular that there are at least 1,487 itest plugins on PyPI.

Where did I get that number? If you go to the pytest.org, there is a site, there is a pytest plugin list that lists 1,487 plugins. This is done by searching PyPI for pytest. Actually, projects that begin with pytest- or pytest- since that is a common convention. If you go directly to PyPI, you can just search for pytest, and it comes up with

10,000 plus projects. That's a little overwhelming. So while I appreciate the restriction down to 1,487, that's still kind of a lot. So what should we do? An alternative list is on pythontest.com. There's a list I put together called Top PyTest Plugins, and that is based on the counts for the number of downloads over the past 30 days-ish, and

The list in there has currently 133 plugins. Although popularity isn't everything, I was curious about the most downloaded Bytes plugins, and that's why I put this list together.

I actually used another data set from a website from Hugo VK called Top PyPI Packages. And this is regularly updated about once a month to dump the 8,000 most downloaded packages from PyPI. And I grabbed all 8,000 and searched for pytest.py.

in that list and I do some exclusions. So there are some packages that are not supported anymore so there's no point in looking at them. And there's also PyTest itself which isn't a plugin so those are removed from the list. And that's why right now there's 133 items but that could change over time. So this list and PyTest plugins in general is what we're going to focus on for this season.

We're not going to go through all 133, but we're going to go through quite a few. And we're also not going to restrict ourselves to just this. There are other plugins that are super cool that might not be on this list. I'd like you to share your favorite plugins, whether on this list or not, that you would like to, or plugins that you'd like to have me take a look at, and we can talk about it on the show. If you're a plugin fan,

author and you really think that your plugin should you'd want to talk about either being an author of pytest plugins or this a particular plugin or two or three let me know i'd be happy to try to get some people on the show

So I've got this list of the top plugins from 1 to 133, and it's not going to be ordered by top to bottom, but I might start with an ordering and then jump around a bit, depending on popularity of myself. The things that I use, I can talk about easily. Some things I might have to do a little bit of research with. And...

Some, I just am not interested. Also, I really don't want to do 133 episodes in this season, but we'll see. Who knows? Might be longer, but probably not. For instance, let's take a look at the top.

The number one downloaded plugin is PyTestCov. And that is a PyTest plugin for measuring coverage. It actually uses Coverage.py and it provides some conveniences. And I won't get too much more into it because I want to talk about that in the next episode. So we'll use PyTestCov in the next episode. But there's a whole bunch of great ones in here. Let's take a look down the list.

Currently number four is PyTest AsyncIO. That sounds cool. I don't write a lot of async code, but that'd be one that I'd love to talk to somebody about, either the plugin author or anybody that uses it on a regular basis and wants to talk to me. That'd be great. PyTest Metadata I use all the time, and that's along with PyTest HTML, but Metadata is usable by itself. And those are great. Those are number five and six. PyTest Timeout is fun.

skim down a bit. PyTest ordering, there's various ones for ordering that I'd like to kind of like talk about those together that might make sense. There is PyTest repeat for repeating tests. That's one that I've, I have helped with in maintenance before. PyTest check is my plugin. That's number 24. That's one of mine. It allows multiple features, multiple failures per test case.

PyTest Playwright. This is on my list of things I want to try because I'm working on a web project. So I do definitely want to try PyTest Playwright, but it'd be fun to talk to somebody else about that. What else? Let's go down. What's near the bottom? There is PyTest MD Report at 133. Now that's not the least downloaded one. That's just the least downloaded in my list. But

It still gets, looks like, 112,000 downloads per month. So PyTest plugins are downloaded a lot. So this one is a PyTest plugin to generate list test outcome reports with markdown table format. Cool. So reporting the outcomes in a markdown format. This would be cool if you're piping the output to somewhere where you can read it better or...

That can, yeah, maybe to a website or something. That's cool. I'll definitely check this one out. And one of the things that if I, so I'm looking at this list and what I've done with this list is it's got the number, the order number, the name of the plugin, plus a link to the plugin in the name and the number of downloads. And, and the, so I'm getting that all right from the top PyPI packages.

And then I also wanted to have like the description. So this one says a PyTest plugin to generate test outcome reports with Markdown table format. And I'm getting that directly from PyPI. So there's an API to PyPI to pull that out. So all of those descriptions and the name and links, those are all on the top PyTest plugin list.

And yeah, that's partly where I'm getting this. Like I said, I will cover other stuff too. Popularity isn't everything. The top of the list is going to be not too surprising. Let's actually talk about some of the other, the top. Let's go through the top nine because these are all ones that I'm definitely going to talk about. Probably the top. Yeah, more than that. But top nine are PyTestCov, PyTestMock, XDist, AsyncIO, Metadata, HTML, Timeout, RerunFailures,

And number nine is PyTest Django. And I definitely will talk about that because I'm working on a Django project. So these will all get covered, but I'm not going to. And then I don't know if I will do it all right away or jump around. Who knows what I will think about doing. So that's covering pre-existing plugins, which is, there's a few reasons why I want to do this. One, I want to let people know about these without having to go find them themselves.

Two, I want to plug my top PyTest plugin list, but I'm not making any money from it, so I don't know why.

Three, it's just good to like sort of hear about some other things. And the episodes might be short. So I might have some short episodes because I don't know much about something. But I don't want to like lump things together too much. If there's like there's a whole bunch of random ones, we'll probably do those together. We might do those together. And then there's different ordering ones that might do those together. Because it makes sense when we're thinking about

If you're going to reorder stuff, which ordering plugin to use, that might make sense to lump those into one episode. But a lot of them might be just individual plugins that we'll take a look at. Now, there's a lot of plugins that you should take a look at, even if you're not going to use, because...

they either will show you some cool use of fixtures or some cool use of the hook functions. So I find that learning how to use the hook functions from examples using the hook functions is the best way. Now there's the PyTest documentation and it's really good, but around hook functions and how to use hook functions, I think it might still be lacking. And this is something that like,

I think how to write PyTest plugins, that would be actually a decent course or a book. I do have a section of the Python Testing with PyTest and the Complete PyTest course, so both in book form and in video course form. I do cover building plugins, but I don't go through all of the hook functions, just a few.

So, opportunities there for people to jump on board. Anyway, this introduction is getting long. I'm going to talk about not just some plugins that you need to know about, but plugins that you might want to look at for their source code, and also some

Also might talk about some of these hook functions and how to utilize those and what they do. That might be a fun thing to do in this series. And I'm not sure where this series will go, but I thought it'd be fun to do a season of PyTest plugins. So, welcome. Thanks.

Thank you for listening and thank you to everyone who has supported the show through purchases of the courses, both Hello PyTest, the new fastest way to learn PyTest, and the complete PyTest course, if you'd like to really become an expert at PyTest. Both are available at courses.pythontest.com and there you can also join the Python test community. That's all for now. Now go out and test something.