avatarUmangshrestha

Summary

The web content provides a guide on customizing HTML reports in Pytest to enhance their presentation and utility for various audiences.

Abstract

The article titled "How to Customize an HTML Report in Pytest" offers a comprehensive walkthrough for developers looking to tailor their test report outputs. It begins by emphasizing the importance of not just performing tests but also effectively communicating the results through well-presented reports. The guide assumes basic Pytest knowledge and starts with setting up a virtual environment and installing necessary packages, including pytest and pytest-html. It then proceeds to demonstrate how to generate a basic HTML report and outlines methods for customizing elements such as the report title, environment information, summary sections, and table headers and rows. The article includes code snippets and before-and-after screenshots to illustrate the customization process, aiming to help readers create more informative and visually appealing reports for stakeholders.

Opinions

  • The author suggests that creating a customized HTML report for Pytest runs is crucial for advertising the work done, broadcasting results to managers or team members, and for self-analysis or presentations.
  • The article implies that the default Pytest HTML report is functional but aesthetically lacking, which motivates the need for customization.
  • Customization is presented as a way to make the report more engaging and relevant to the audience, whether for immediate use or for future reference during regression analysis.
  • The author provides a subjective assessment that the customization process, while adding additional steps, is worth the effort for the enhanced communication of test results.
  • The article endorses the use of an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus (GPT-4), suggesting it as a valuable tool for users interested in such technologies.

How to Customize an HTML Report in Pytest

Make the report you want to make.

Bland pytest HTML report

They say that half the work is doing something, and the other half is advertising. You would have spent hours writing unique and critical test cases to increase the reliability of the system. If you spend a little more time, then you can create a proper report for your pytest runs, customized to your need.

With this, you can broadcast the report to your manager or team member, use it for presentation or self-analysis, or even save it later for analyzing regression. This article assumes that you are slightly familiar with basic pytest concepts. If you don’t know or want to refresh your knowledge, then quickly glance over this introductory article here.

Installation

Let’s start by installing and activating the virtual environment:

$ mkdir customizing-pytest-report
$ virtualenv venv
$ source venv/bin/activate

Now install the HTML report generator.

$ sudo apt install pytest
$ pip install pytest
$ pip install pytest-html

Generating HTML report

Folder structure

To get the output, that is, to get the pytest report:

$ pytest --html=report.html
html report

As you see, the pytest report is a little bland. But it allows customization. We will customize the elements a section at a time.

Changing Title

We change the title in the HTML report by overwriting the pytest_html_report_title function. To change the title, just add the following in conftest.py. The function will be called before adding the title to the report.

Title changed to "Custom Title" in html report

Changing Environment

Environment parameters are in dict format. We change the _metadata in pytest_configure. For simple example refer below:

Environment changed in html report

Changing Summary

There prefix, summary and postfix are a list of HTML elements. To make changes to them, we add or remove elements. The pass and fail in summary, you see, are a part of the summary array.

Summary changed in html report

Changing Table Headers

Cells are the list of HTML elements that are taken as header. We just have to add or remove the cells. And in the end, do a pop.

Table headers changed in Results

Changing Table Rows

The result of the pytest has the following information:

<TestReport ‘tests/test_case.py::test_min[input1–7]’ when=’setup’ outcome=’passed’>

To change the reports, we have to modify the output of the pytest report in pytest_runtest_makereport and to orient the result in the HTML table, we edit in pytest_html_results_table_rowuse.

Table row changed in Results

Now you know the foundational knowledge to create and customize the HTML report to share and impress other people.

More content at plainenglish.io

Python
Programming
Pytest
Software Development
HTML
Recommended from ReadMedium