Gao Fushuai in the unit testing world, Pytest Framework (End) Test Report

The first three chapters introduced you to pytest's use case writing, pre- and post-processing methods, use case marking and other methods. This chapter mainly introduces how pytest integrates test reports. Pytest itself does not have the function of generating test reports, but there are many plug-ins in pytest through which we can generate test reports. Below we will introduce two ways to generate reports. One is to generate html reports, and the other is to integrate the allure reporting platform to display test reports.

1. Generate HTML report

There are many plug-ins for pytest to generate HTML, such as pytest-html, pytest-testreport, etc. Here I will introduce how to use the pytest-testreport plug-in to generate HTML test reports.

1.1. Install plug-in

pip install pytest-testreport

Note: If you have installed the pytest-html plug-in, please uninstall it first, otherwise there may be conflicts.

1.2. Introduction to the use of plug-ins

When using pytest to run tests, if you want to use pytest-testreport to generate a test report, add the parameter --report to specify the report file name when running the test.

Other operating parameters:

--title: Specify the report title

--tester: Specify the tester in the report

--desc: Specify 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
  • Use pytest.main to execute in the code
import pytest

pytest.main(['--report=musen.html',
             '--title=上课报告',
             '--tester=测试员',
             '--desc=报告描述信息',
             '--template=2'])

The generated report format is as follows

Style one

Style 2

This is the introduction to report generation in HTML format. Next, 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: Address:
https://github.com/allure-framework/allure2/releases

After downloading and unzipping

2. Environment variable configuration

Put the bin directory path of the allure path after allure is decompressed into the environment variable

3. Install allure’s pytest plug-in

pip install allure-pytest 

2.2. Generate allure report

After installing and configuring the allure environment, when using pytest to execute the use case, you can specify to generate allure reports through the parameters of the allure plug-in.

Operating parameters:

--alluredir: Specify the path where allure reports are saved

  • Run from command line
pytest --alluredir=reports
  • Use pytest.main to execute in the code
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 is started, the browser will automatically open and the allure service page will be displayed.

2.4. Allure has built-in commonly used methods.

Add error screenshot

  • allure.title
 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 use case name in report

  • allure.title import allure class TestLogin: @allure.title('登录用例') def test_login(self): pass

Dynamically set the use case name in parameterized use cases

  • allure.dynamic.title # Use case data casedatas = [ {'title': 'Reverse use case 1','data':"xxx"}, {'title': 'Reverse use case 2','data':"xxx "}, {'title': 'Reverse Use Case 3','data':"xxx"} ] class TestLogin: @pytest.mark.parametrize('item',casedatas ) def test_demo(self, item): # Dynamic Set the use case name in the report allure.dynamic.title(item['title'])

Add feature descriptions in reports

  • allure.story
@allure.story('登录功能')
class TestLogin:

    @allure.title('登录用例')
    def test_login(self):
        pass

Add package name in report

  • allure.suite
@allure.suite('登录测试套件')
class TestLogin:
    @allure.title('登录用例')
    def test_login(self):
        pass

The function of pytest to generate test reports has been extended here!

Guess you like

Origin blog.csdn.net/a448335587/article/details/132839049