Generate Test Report for in PyTest Using pytest-html, Allure

After testing different test cases including APIs, parallel testing, Data-driven testing in PyTest. it is necessary to keep a record of test results generated by different test cases.

PyTest provides another most efficient functionality of generating test results in form of reports to keep a proper record of test cases and their results. In this tutorial, we will be generating our test reports in HTML, Allure, and Junit.

Prerequisites – Automation test in pytest

Let’s start with the Test Case

Create a python pytest file with the name test_parallel.py. Add pytest code to test_parallel.py

import pytest
def test_1():
    return 'hello'
    assert True
    
def test_2():
    assert 37 in {
        num
        for num in range(1, 50)
        if num != 1 and not any([num % div == 0 for div in range(2, num)])
    }

def test_3():
    s = 'hey'
    assert 'hey'==s

Execute the above test cases using the command pytest test_parallel.py. Hence the test case passed successfully.

Generate Test Report for in PyTest Using Allure and jUnit

we can generate a test report in HTML using the pytest framework by installing a plugin. for installation of the plugin, we have to execute a command pip install pytest-html. with the help of this plugin, we will be generating HTML reports.

Next, generate a report of test_parallel.py in HTML by using the command pytest –html=report.html. After executing the command we can see “report.html” is created in our working directory.

Generate test report using Allure in Python

Now if we want to generate a test report in Allure using the pytest framework we have to install Allure. for installation of the Allure, we have to execute a command pip install allure-pytest. with the help of this framework, we will be generating allure reports.

Download the latest allure package from the allure framework. unzip the file and copy the path to the bin folder and add it to the path environment variable.

Next, generate a report of test_parallel.py in allure by using the command pytest –alluredir=allure-report/.

we can view our allure-report in the default browser by using the command allure serve allure-report/.

Generate test report in Junit

If we want to generate an XML report using Junit we have to simply run a command pytest test_parallel.py –junitxml=“report.xml”.

After executing the command we can see “report.xml” is created in our working directory.

Thus, In this tutorial, we have learned how to generate pytest reports using HTML, Allure, Junit.