1. Generate HTML report
There are many plug-ins for pytest to generate HTML, such as pytest-html, pytest-testreport, etc. The following will introduce how to use the plug-in pytest-testreport to generate HTML test reports.
1.1. Install the plug-in
pip install pytest-testreport
Note: If you have installed the pytest-html plugin, please uninstall it first, otherwise there may be conflicts
1.2. Introduction to the use of plug-ins
When using pytest to run a test, if you want to use pytest-testreport to generate a test report, you can add the parameter --report to specify the report file name when running the test, and you can achieve it.
Other operating parameters:
--title : specify the report title
--tester : specify the tester in the report
--desc : Specifies the item description in the report
--template : Specify report template style (1 or 2)
-
Command line execution:
pytest --report=musen.html --title=测试报告 --tester=木森 --desc=项目描述 --template=2
If you want to learn automated testing, here I recommend a set of videos for you. This video can be said to be the first interface automation testing tutorial on the entire network at station B. At the same time, the number of online users has reached 1,000, and there are notes to collect and various Lu Dashen Technical Exchange: 798478386
-
The code is executed using pytest.main
import pytest
pytest.main(['--report=musen.html',
'--title=柠檬班上课报告',
'--tester=测试员',
'--desc=报告描述信息',
'--template=2'])
-
The generated report style is as follows
style one
style two
The report generation in HTML format will be introduced here, and then I will tell you how to integrate the allure report
2. Integrate Allure report
If you want to integrate allure reports in pytest, you must first download allure and configure the environment
2.1, allure environment configuration
1. Download allure:
地址:https://github.com/allure-framework/allure2/releases
After downloading and decompressing
2. Environment variable configuration
Put the bin directory path of the allure path after allure decompression into the environment variable
3. Install allure's pytest plugin
pip install allure-pytest
2.2. Generate allure report
After installing and configuring the allure environment, when using pytest to execute the test case, you can specify to generate allure to report through the parameters of the allure plug-in.
Operating parameters:
--alluredir : Specify the path where allure reports are saved
-
command line run
pytest --alluredir=reports
-
The code is executed using pytest.main
import pytest
pytest.main(['--alluredir=reports'])
2.3. Start allure service
Enter the following command in the command terminal to start the allure service
# 命令:allure serve 生成的报告路径
allure serve reports
Execute the above command, after the allure service starts, it will automatically open the browser and display the allure service page
2.4, allure built-in commonly used methods
Add error screenshot
-
allure.attach
def error_save_screenshot(driver,file_path, desc):
# 对当前页web页面进行截图
driver.save_screenshot(file_path)
# 将截图保存到allure报告中
with open(file_path, "rb") as f:
file = f.read()
allure.attach(file, "失败截图", allure.attachment_type.PNG)
Add the use case name in the report
-
allure.title
import allure
class TestLogin:
@allure.title('登录用例')
def test_login(self):
pass
Dynamically set the use case name in the parameterized use case
-
allure.dynamic.title
# 用例数据
casedatas = [
{'title': '反向用例1','data':"xxx"},
{'title': '反向用例2','data':"xxx"},
{'title': '反向用例3','data':"xxx"}
]
class TestLogin:
@pytest.mark.parametrize('item',casedatas )
def test_demo(self, item):
# 动态设置报告中的用例名称
allure.dynamic.title(item['title'])
Add feature description in report
-
allure.story
@allure.story('登录功能')
class TestLogin:
@allure.title('登录用例')
def test_login(self):
pass
Add the package name in the report
-
allure.suite
@allure.suite('登录测试套件')
class TestLogin:
@allure.title('登录用例')
def test_login(self):
pass