python + pytest + allure generate test report

1. Environmental preparation

1. Download allure address:

Method 1: git download address https://github.com/allure-framework/allure2/releases

Find the version you want to download

Method 2: Download address: Central Repository: io/qameta/allure/allure-commandline/2.17.3 https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/2.17.3/
Choose a version:

2. Configure allure environment variables

Unzip the downloaded allure file and find the bin file directory path

mac:

        Put the path in .bash_profile, as follows:

PATH="/Users/XXX/allure-2.17.3/bin:${PATH}"
export PATH

        or:

export /Users/XXX/allure-2.17.3/bin

        Then:

source ~/.bash_profile

        Windows: Just configure it in the environment variable

        Then restart pycharm

3. Allure needs jdk support to run, so remember to install jdk

        Download and install online

4. Install the allure-pytest plugin

        Can be installed via pycharm

        It can also be installed with pip install allure-pytest


two,

Use the allure-pytest plugin to generate a test report file in html format

1. pytest --alluredir=report (folder) xxx.py

The execution result is printed on the terminal, and a report folder is generated at the same time, which contains the test results in json format.

If it keeps being generated, it will always be superimposed -----> clear the directory first, and then generate the test results:

pytest --alluredir=report --clean-alluredir xxx.py

2. Generate a test report in HTML format:

①allure generate report (folder) -----> stored in the allure-report folder by default

②allure generate report -o test_report----->Specify to store in the test_report folder

If the test_report folder already exists:

allure generate -c report -o test_report


3. How to execute through code, how to refine the test report through decorators

1、case 

import pytest
import allure
 
# 设置一条测试用例的每个步骤(方法1)
@allure.step("测试第一步")
def step_1():
    pass
 
@allure.step("测试第二步")
def step_2():
    pass
 
# 按照模块子模块或功能点子功能点, 对测试用例进行分组
@allure.epic('后台管理模块')
@allure.feature('登录功能点')
@allure.story('正常登录')
def test_a():
    # 设置一条测试用例的每个步骤(方法2)
    with allure.step("测试第一步"):
        pass
 
    with allure.step("测试第二步"):
        assert 1 == 2
 
    with allure.step("测试第三步"):
        pass
 
@allure.epic('后台管理模块')
@allure.feature('登录功能点')
@allure.story('用户名错误登录')
@allure.issue('http://127.0.0.1:80/zantaopms/')
@allure.testcase('http://www.baidu.com/')
# 设置测试用例的标签, 可以设置多个
@allure.tag("回归测试", "重要")
# 设置测试用例的级别  blocker > critical > normal > minor > trivial
@allure.title('测试登录:用户名错误')
@allure.description('测试登录:测试用例描述信息')
@allure.severity("blocker")
def test_b1():
    step_1()
    step_2()
 
@allure.epic('后台管理模块')
@allure.feature('商品管理')
def test_c():
    assert 1 == 1
 
def test_d():
    assert 1 == 2
 
def test_e():
    assert 1 == 2

2. Create a framework entry

import pytest
import os
 
if __name__ == '__main__':
    result_dir = "./test_result"
    report_dir = "./test_report"
    # pytest.main() 相当于执行pytest命令
    pytest.main(["-sv", "--alluredir=%s"%result_dir, "--clean-alluredir", "test_allure_markers.py"])
    ret = os.system("allure generate --clean %s -o %s" % (result_dir, report_dir))
    if ret:
        print('生成测试报告失败')
    else:
        print('生成测试报告成功')


 

4. Results display

5. Some functions of allure

Mark test points: @allure.feature can be used to customize the test class title, such as: login

Mark test case: @allure.story can be used to customize the function method title, such as: login success

Test Cases - Parameterized/Data-Driven: @pytest.mark.parametrise

Test Step: @allure.step

@pytest.allure.step: It can be used to define the call step name for the function method. po call - step description. (Recommendation: po hierarchical design, call function method and use decorator to specify step name)

with allure.step: Record the step at the specified location, and the statement block contained in with is the operation that the step should perform.

allure.attach: add an attachment to the annotation, such as a screenshot. allure. attach(name, body, type)

@allure.issue("url"): Associated bugs, such as: Zen Road xxbug

@allure.testcase("url"): associated case, such as: Zen Road xxcase
 

Guess you like

Origin blog.csdn.net/Jiazengzeng/article/details/123074297