pytest-metadata - provides access to test session metadata

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. 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 dirt 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 Jason 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 in 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 data 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 a CI 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 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 pytestHTML 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 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-reporter-html1. 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-html1. 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.

Creators and Guests

Brian Okken
Host
Brian Okken
Software Engineer, also on Python Bytes and Python People podcasts
pytest-metadata - provides access to test session metadata
Broadcast by