Generating HTML Test Reports in Python



We can generate HTML reports with our Selenium test using the Pytest Testing Framework. To configure Pytest, we have to run the following command:

pip install pytest.

Once the installation is done, we can run the command to check the Pytest version installed −

pytest –version

As a Pytest standard, the Python file containing the Pytest should start with test_ or end with _test. Also, all the test steps should be within a method whose name should start with test_.

To run a Pytest file, we can open the terminal and move from the current directory to the directory of the Pytest file that we want to execute. Then, run the command mentioned below −

py.test -v -s.

Let us look at a project structure following the Pytest Test Framework.

Pytest Test Framework

In the above image, it shows that the Pytest file has the name test_p.py and it contains a test method with the name test_SeleniumTest.

To generate a HTML report for a Selenium test, we have to install a plugin with the command: pip install pytest-html. To generate the report, we have to move from the current directory to the directory of the Pytest file that we want to execute. Then run the command: pytest --html=report.html.

After this command is successfully executed, a new file called the report.html gets generated within the project.

Python Project Test

Right-click on the report.html and select the option Copy Path.

Report HTML

Open the path of the file copied in a browser, to get the HTML report.

Report

The HTML report gives information of the Environment on which the test is executed. It also contains the information on test Summary and Results.

Advertisements