Creating Pytest HTML Test Reports

In this short but useful tutorial, we'll learn how to install and configure pytest-html plugin to generate some nice reports for our test project.

When pytest runs at the command line, it sometimes prints an unhelpful wall of text. Visual reports are a much better way to consume test result information, especially for non-developers. Adding the pytest-html plugin to your test project enables you to print pretty HTML reports with one simple command line option.

Installing pytest-html

Installing pytest-html is a very simple task, we just use the command below:

pipenv install pytest-html --dev

That's all for the installation! It comes with a very simple yet useful out-of-the-box setup.

Using pytest-html reports

To generate the reports for our execution, we just include the --html flag in the execution command with the name (including the extension) of the file we want to generate (in HTML format) and the report will be generated in the folder where the execution was triggered unless we specify a specific path.

Executing the test with the HTML report generation:

pipenv run python -m pytest tests/test_web.py --html=report.html

There are additional configurations that we can use to manipulate our report, which can be found in the official User Guide