PyTest HTML has got to be one of my all-time favorite plugins. PyTest HTML is a plugin for PyTest that generates an HTML report for test results. This episode digs into some of the super coolness of this plugin. Welcome to Test & Code. This episode is brought to you by others like you that have signed up for a course or two at courses.pythontest.com.
There are a few changes over there. The complete PyTest course is now the complete PyTest course bundle. It's been split into three separate courses, since you really need them at different times in your PyTest journey. PyTest Primary Power provides in-depth training on test functions, fixtures, parameterization, and markers. Really the superpowers of PyTest that you need to learn to use PyTest effectively.
Then there's using PyTest with projects with more strategic and when you need it topics like debugging, testing strategy, mocking. And when you need advanced parameterization techniques or want to share code between projects with plugins, reach for PyTest Booster Rockets. A separate course, Hello PyTest, focuses on core PyTest features needed to get started fast. Great for teams where you want everyone to get to a good base level of PyTest scope.
These are all at courses.pythontest.com. Getting started with PyTest HTML is easy enough. You just run PyTest with an HTML flag and give it a file name, like something like report.html or something, and it will generate a report. By default, it actually puts a bunch of the extra stuff, like the CSS and everything, into a directory. But you can pass in a flag, self-contained HTML, and it will stick all of that into one file.
So you just have an HTML file as output. That's what I usually do. That approach also works great if you're going to move the report somewhere else, like email it to somebody or whatever. The report has some environmental info at the top and a summary of the test run, things like number of tests, time it took,
and the numbers of all the result types like past, skipped, failed, errors, etc. And then you've got a results table. This is a table with result status and those are in color too. So the failures are in red and passes are in green and stuff. And then you've got the test name and duration. And I actually don't remember what the default columns are because I often customize this table. The table columns are all sortable too. So you can sort by result or by test name or duration or whatever.
And sorting by result means like you click at the top and you'll get like all the fails at the top or something. But you can also use the table to find what if you've got the durations, you can find what the slower tests are or the faster tests. For anything with extra information like errors or failures and stuff, you can click on the result and it expands into a block that shows the traceback or any information around there.
And if you haven't seen this before, you got to try this out. It's super helpful even if you're just debugging a big suite locally, but it also works great to link to in a CI server or something like that. There's also a screenshot of the report in the repo read me so you can check it out there if you don't really want to give it a try right now.
but I highly recommend trying it. One of the cool things about PyTest HTML is the customization. The docs are really clear and helpful regarding customization. And I'll have a link in the show notes to the docs as well. You can customize the CSS and you can add stuff to the environment section. For this, there's another plugin called PyTest Metadata that makes that super easy. This extra data gets passed in on the command line or even
at runtime with PyTest hooks. For the table, for the results table, you can add columns, you can customize existing columns, you can hide columns, you can add stuff like links, extra data, and even pictures. I've seen the pictures used where somebody will take a screenshot during the test, and if the test fails, they'll attach that to the report. That's super cool, super helpful.
Remember when I said there was an environment and summary section at the top of the results table? Let's go back up there now. The environment section, by default, has Python version, platform, which operating system you're running on, PyTest and plug-in version, and which PyTest plugins are present. But you can add to that section. The easiest way is to use the PyTest metadata plugin.
That's a different plugin, which is cool enough on its own to deserve its own episode. But it gets installed also when you install PyTest HTML. And you can use that to extend the environment report from the command line or from hook functions or from fixtures. You can even remove some of the default stuff if you don't want it there.
I like to add things like code versions, hardware options, device configurations of the thing I'm testing. You can put whatever you want there that's helpful to have at the top of the report and you can do it very easily. Now let's look at just under that at the summary section. You can also add to the summary via a pytest HTML results summary hook function.
That hug function gets past three parameters, a prefix, summary, and postfix, which are just lists that you can add to HTML and that shows up in the report. For me, I don't want to clutter the default view too much, so I usually stick the extra summary info into a details HTML block with a summary line, and it just shows up as a single line that someone can click on to see more info.
I just used this feature recently to include some aggregate test data collected at runtime into the report. Having it collapsed assures that only people that care about it need to look at it. But also, being able to control the HTML makes it super easy to present it exactly how you want it. Anyway, I love PyTest HTML. Check it out if you haven't already. Thanks for listening. Thanks to everyone who's supported the show by picking up a course or two at courses.pythontest.com.
Whether you need to get started with PyTest today or want to power up your PyTest skills, I've got a course for you. That's all for today. Now go out and test something.