Use Pytest to integrate Allure to generate beautiful graphical test reports

Table of contents

foreword

Dependency package installation

Pytest

Allure Pytest Adaptor

Retrofit Pytest-based test cases

Generate test report

run test

Generate test report

open test report

 Data acquisition method


foreword

I wrote a blog about generating test reports before, but in fact, Allure is first a test report generation framework that can run independently, and then there is an integrated plug-in for Jenkins.

This time mainly introduces how to integrate Allure into Python's Pytest unit testing framework.

Dependency package installation

Pytest

Pytest is a unit testing framework for Python, which is very convenient and easy to use. It is possible to scale and write more complex test cases. The installation method is as follows:

pip install pytest

Allure Pytest Adaptor

Allure Pytest Adapter is a plug-in for Pytest, through which we can generate the data required by Allure for generating test reports. The installation method is as follows:

pip install pytest-allure-adaptor

Retrofit Pytest-based test cases

In order to use Allure to generate reports, you need to add Allure features to conftest.py and test scripts. The functions of pytest-allure-adaptor are introduced in detail in the official website of pytest-allure-adaptor. This time, we will start from reality and introduce how to apply it to our own framework.

First of all, in conftest.py, the information of the test environment can be output to the report through the allure.environment method, such as adding the host used for the test and the browser used for the test to the test report:

#!/usr/bin/env python
# coding=utf-8

import pytest
import allure
import yaml

@pytest.fixture(scope="session", autouse=True)
def env(request):
    """
    Parse env config info
    """
    root_dir = request.config.rootdir
    config_path = '{0}/config/env_config.yml'.format(root_dir)
    with open(config_path) as f:
        env_config = yaml.load(f) # 读取配置文件

    allure.environment(host=env_config['host']['domain']) # 测试报告中展示host
    allure.environment(browser=env_config['host']['browser']) # 测试报告中展示browser

    return env_config

Next, in the test script, add the allure feature, and look directly at the script below. I will explain the purpose of the allure feature by adding comments in the script. For example, the test script is test_shopping_trolley.py:

#!/usr/bin/env python
# coding=utf-8

import pytest
import allure


@allure.feature('购物车功能')  # feature定义功能
class TestShoppingTrolley(object):
    @allure.story('加入购物车')  # story定义用户场景
    def test_add_shopping_trolley(self):
        login('刘春明', '密码')  # 调用“步骤函数”
        with allure.step("浏览商品"):  # 将一个测试用例分成几个步骤,将步骤打印到测试报告中,步骤2
            allure.attach('商品1', '刘春明')  # attach可以打印一些附加信息
            allure.attach('商品2', 'liuchunming')
        with allure.step("点击商品"):  # 将一个测试用例分成几个步骤,将步骤打印到测试报告中,步骤3
            pass
        with allure.step("校验结果"):
            allure.attach('期望结果', '添加购物车成功')
            allure.attach('实际结果', '添加购物车失败')
            assert 'success' == 'failed'

    @allure.story('修改购物车')
    def test_edit_shopping_trolley(self):
        pass

    @pytest.mark.skipif(reason='本次不执行')
    @allure.story('删除购物车')
    def test_delete_shopping_trolley(self):
        pass


@allure.step('用户登录')  # 还可以将一个函数作为一个步骤,调用此函数时,报告中输出一个步骤,步骤名字通常是函数名,我把这样的函数叫“步骤函数”
def login(user, pwd):
    print(user, pwd)

Several features of Allure are used above:

  • @allure.feature # Used to define the function to be tested, the demand point of the product under test
  • @allure.story # Used to define the user scenario of the function under test, that is, the sub-function point
  • with allure.step # Used to divide a test case into several steps and output it in the report
  • allure.attach # Used to enter some additional information into the test report, usually some test data information
  • @pytest.allure.step # Used to output some common functions as test steps to the report, where the function is called, the steps will be output to the report

Generate test report

After the Allure feature is added to the test script, it is necessary to generate the test result data required by the Allure report when executing the test. When py.test executes the test, specify the --alluredir option and the directory where the test data is saved:

run test

py.test test/ --alluredir ./result/

The result data of this test is saved in ./result/. In addition, you can also execute specified features or stories to execute some test cases, such as executing the test cases of the "Add to Cart" sub-function under the "Shopping Cart Function":

py.test test/ --allure_features='购物车功能' --allure_stories='加入购物车'

Generate test report

Next, we can use the test data generated by the above command to generate an Allure test report using the command line. The command is as follows:

allure generate ./result/ -o ./report/ --clean

open test report

Open the test report with the following command:

allure open -h 127.0.0.1 -p 8083 ./report/

The test report will be opened with the default browser. The following figure is a graphic example on github


 Data acquisition method

【Message 777】

Friends who want to get source code and other tutorial materials, please like + comment + bookmark , triple!

After three times in a row , I will send you private messages one by one in the comment area~

Guess you like

Origin blog.csdn.net/GDYY3721/article/details/132164898