Last Updated On
In this tutorial, we will create a HTML Report in PyTest Framework. pytest-html is a plugin for pytest that generates a HTML report for test results.
Prerequisite:
- Python is installed on the machine
- PIP is installed on the machine
- PyCharm or other Python Editor is installed on the machine
If you need help in installing Python, please refer to this tutorial – How to Install Python on Windows 11.
If you need help in installing PyCharms, please refer to this tutorial – How to install PyCharms on Windows 11.
Implementation Steps
Step 1 – Install pytest
pip install -U pytest
Step 2 – Install pytest-html
pip install pytest-html
![](https://qaautomation.expert/wp-content/uploads/2024/01/image-173.png?w=1111)
Step 3 – Create a new project folder and open it in PyCharm.
![](https://qaautomation.expert/wp-content/uploads/2024/01/image-165.png?w=351)
Step 4 – Go to the project folder and create a new python file – sample_test.py.
![](https://qaautomation.expert/wp-content/uploads/2024/01/image-166.png?w=344)
Step 5 – Add pytest-html package to the PyCharms
Go to File->Settings ->Project: PyTest_Framework->Python Interpreter.
Click on the “+” sign and enter allure-r in the search bar. It will show a list of packages. Select the “pytest-html” package and click on the “Install Package”.
![](https://qaautomation.expert/wp-content/uploads/2024/01/image-167.png?w=802)
Once the package is installed, we will see the message that the package is installed successfully.
![](https://qaautomation.expert/wp-content/uploads/2024/01/image-168.png?w=897)
Step 6 – Create the test.
import pytest
def test_addition():
a = 6
b = 5
c = 11
assert a + b == 11, "Sum is not 11"
assert a + b == c, "Sum of a and b is not equal to c"
def test_subtraction():
x = 15
y = 4
z = 10
assert x - y == z, "Subtract y from x is equal to z"
def test_multiplication():
a = 6
b = 5
c = 30
assert a * b == c, "Product of 5 and 6 is not 30"
def test_division():
a = 16
b = 2
c = 8
assert a / b == c, "Division of 2 by 6 is not 3"
Step 7 – To run this test, use the below command:
pytest --html=report.html
The output of the above execution is
![](https://qaautomation.expert/wp-content/uploads/2024/01/image-170.png?w=1200)
The report.html report will be generated in the root directory.
![](https://qaautomation.expert/wp-content/uploads/2024/01/image-169.png?w=331)
Step 8 – Generating HTML report
Right-click and open with Web Browser.
![](https://qaautomation.expert/wp-content/uploads/2024/01/image-172.png?w=1200)
That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!