We're sunsetting PodQuest on 2025-07-28. Thank you for your support!
Export Podcast Subscriptions
cover of episode pytest-html - a plugin that generates HTML reports for test results

pytest-html - a plugin that generates HTML reports for test results

2025/3/27
logo of podcast Test & Code

Test & Code

AI Deep Dive AI Chapters Transcript
People
B
Brian Okken
Topics
Brian Okken: pytest-html插件是我最喜欢的pytest插件之一,它能够生成简洁易懂的HTML测试报告,方便查看测试结果。该插件易于使用,只需在运行pytest时添加简单的命令行参数即可生成报告,并且可以自定义报告的输出格式,例如,可以选择将所有内容都包含在一个HTML文件中,或者将CSS等额外内容放在单独的目录中。 生成的报告包含环境信息,例如Python版本、操作系统和使用的插件等,以及测试运行的摘要,包括测试数量、耗时和各种结果类型(例如通过、失败、跳过等)的数量。报告中最重要的部分是结果表格,该表格以颜色区分不同的结果状态(例如通过为绿色,失败为红色),并显示测试名称和持续时间。表格中的列还可以排序,方便查找特定信息,例如查找耗时最长的测试。 点击结果表格中的结果可以展开显示详细的信息,例如回溯信息,这对于调试失败的测试非常有用。该报告可以用于本地调试,也可以链接到CI服务器中。 pytest-html插件的一个重要特性是其高度的可定制性。其文档清晰易懂,方便用户学习和使用。用户可以自定义CSS样式,并向环境部分添加额外的信息,例如代码版本、硬件配置等。pytest-metadata插件可以简化这个过程。用户还可以自定义结果表格,例如添加、自定义或隐藏列,甚至添加链接、额外数据和图片。 报告顶部的环境部分默认包含一些信息,例如Python版本、平台等,但用户可以使用pytest-metadata插件进行扩展,添加更多有用的信息,或者移除不需要的信息。用户还可以通过pytest_html_results_summary hook函数向摘要部分添加信息,例如将运行时收集的聚合测试数据添加到报告中。为了避免信息过多,可以将额外信息放在一个可折叠的details HTML块中,只显示一行摘要,用户点击后才能查看详细信息。

Deep Dive

Shownotes Transcript

Translations:
中文

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.