We're sunsetting PodQuest on 2025-07-28. Thank you for your support!
Export Podcast Subscriptions
cover of episode pytest-metadata - provides access to test session metadata

pytest-metadata - provides access to test session metadata

2025/5/8
logo of podcast Test & Code

Test & Code

AI Deep Dive Transcript
People
主持人
专注于电动车和能源领域的播客主持人和内容创作者。
Topics
主持人:pytest-metadata 是一个非常有用的 pytest 插件,它允许访问测试会话的元数据。这个插件本身就很有用,即使你已经安装了 pytest-html(它依赖于 pytest-metadata)。 pytest-metadata 默认情况下会添加 Python 版本、平台、已安装的包和插件等信息到测试输出的头部。你可以通过命令行参数 `--metadata` 添加自定义元数据,例如在 CI 环境中添加测试环境信息。你也可以通过 JSON 文件或 fixture 来添加元数据。 你可以通过多种方式读取元数据:直接在 pytest 输出中查看,导出到 JUnit XML 或 JSON 文件,或者在 pytest-html 生成的 HTML 报告中查看。一些其他插件,例如 pytest-base-url、pytest-selenium 和 pytest-reporter-html1,也会向 pytest-metadata 添加元数据。 pytest-metadata 对于在 CI 系统中传递额外的测试信息非常有用,因为它可以方便地将额外的信息与测试结果一起传递到 CI 管道中。

Deep Dive

Shownotes Transcript

Translations:
中文

PyTest Metadata is described as a plugin for PyTest that provides access to test session metadata. That is such a humble description for such a massively useful plugin.

Welcome to Test and Code. This episode is sponsored by porkbun.com, named number one domain registrar by USA Today from 2023 to 2025. Right now, Porkbun has a special low price of less than $6 for the first year of your next .app or .dev domain name. Ideal for developers, web designers, engineers, or anyone in tech.

.app and .dev domains are perfect for your latest projects. Showcase your world-changing application on .app and use .dev for your blog, documentation, and more. As always, every Porkbun domain comes with lots of freebies like SSL certificates, Whois privacy, URL forwarding, and hosting trials.

With deals like this, backed by personalized support from real people 365 days a year, it's no wonder so many tech people and enthusiasts are making the switch from places like GoDaddy and choosing Porkbun instead. To get the first year of your next .app or .dev domain for under $6, visit porkbun.com slash test and code 25 or click the link in your show notes.

If you're already using PyTest HTML, you probably, well, you definitely have PyTest metadata already installed as PyTest metadata is one of the dependencies for PyTest HTML. However, PyTest metadata is very useful even on its own. So what do you get? If you pip install PyTest metadata and just run as normal, you won't really see much difference. However, if you use the dash V flag or the verbose flag with PyTest,

One of the things you'll see right off the bat is at the top with the header of your test data, you'll see a description of there's extra data added. At the top of your PyTest output, you'll see things like what your root directory is and the cache dirve and the test session start line. But metadata will also be there now. Now, metadata, that line is going to be, by default, it adds the Python version.

the platform, the packages, and the plugins. So the platform is an encoded thing that shows you what sort of operating system you're using. On my Mac Mini, I see macOS 14.3.1, ARM 64, stuff like that.

The packages will list all of the Python packages you have installed. And then the plugins, which plugins you have installed also. This already is pretty useful if you want to keep track of that.

However, that metadata section, you can add to it. That's the real beauty of PyTest metadata is adding to this. You can do it on the command line. You can say add a flag of dash dash metadata and then add like foo and bar. You can add as many as you want of these extra things. Why would you do that? So for instance, in CI, there might be extra things like I'm testing against a particular instrument or I'm

testing against a whatever your test environment is in CI, you can pass that information from the command line into the metadata and have that collected with the output. There's other ways to view it later. We're going to talk about that in a minute.

You can also pass it in from JSON. But one of the favorite things I like to do is not pass it in the command line, but pass it in within like a test function or a or then a conf test file and a fixture. You can use a metadata fixture and add information. You can also read so you can pass information back and forth around with this metadata fixture. It's pretty useful for that as well.

For instance, if you're trying to collect some data to post process later, you can pass it through this metadata information. Pretty cool. So how do we read it? So the output, like I said, it comes out directly out the output in a in the header line. However, that is if you're in a pipeline, you're probably not going to use that that much, you might save the output somewhere. But

there's other places it goes. So one of the places it goes is you can set it up to export that to JUnit XML. So if you're using the ACI system that uses the XML output from PyTest, you can set that up and there's information in the readme of this plugin to have the metadata show up in that XML file. Some of the JSON plugins also allow you to export that, that

information to JSON output. It also shows up at the top of the pytest HTML. And in fact, at the end of the readme in pytest metadata, it shows four different plugins that utilize either contribute or read metadata. So there's pytest HTML that displays all the metadata that you pass in at the top of the HTML output.

I'd also like to say that you can modify this too. So if you don't want some of the stuff in there, like some of the defaults, you can take that out. What adds to it?

Interesting, there's a lot of stuff that probably adds to it, but PyTest base URL is one. So if it adds the base URL to the metadata, PyTest Selenium also adds the driver, the capabilities, and remote server to the metadata. And there's another one, PyTest XR.

report a reporter HTML. So this is a new one, actually, new one for me. I need to check this out. It looks like an alternate HTML output for PyTest. But that's PyTest. It's at the bottom of the readme, but it's PyTest reporter HTML 1. That'll display the metadata there too. This might seem like just like

inside baseball stuff, and it kind of is. But if you're working with a CI system or if you're parsing data down the line, it's really helpful to easily be able to pass in extra information with your test results information and pass that down the line in CI or part of the pipeline. Very cool. Very helpful. Glad this is around. Use it all the time.

Thanks to everyone who supported the show via Patreon or by learning PyTest from a course at courses.pythontest.com. I've made a change recently that I'm pretty excited about. The Complete PyTest Course is now the Complete PyTest Course Bundle. It was one big course and is now three courses.

since you really need them at different times in your PyTest journey. Part 1, PyTest Primary Power, provides a gentle introduction up through covering the superpowers of PyTest, including fixtures, parameterization, markers, and more. Grab primary power and get to work. Part 2, using PyTest with projects, has strategic topics like debugging, mocking, continuous integration. Part 2 is perfect for when you're applying PyTest to a work or open source project. Part

Part 3: PyTest Booster Rockets, Explore's plugins, both third-party and building your own,

and advanced parameterization. Although part three can be taken right after the other two, it makes more sense to live with pytest for a while and then explore what more you can do with it. I've also added new intro videos at the beginning of each course, congratulations videos at the end, and printable certificates. Anyway, I think the new structure makes a lot more sense. These are all at courses.pythontest.com. That's all for today. Thanks for listening. Now go ahead and test something.