Python Pytest框架(五)生成测试报告

首先:插件可以pip安装,也可以在pycharm安装:

Python Pytest框架(五)生成测试报告_第1张图片

Python Pytest框架(五)生成测试报告_第2张图片

Python Pytest框架(五)生成测试报告_第3张图片

 

一、使用pytest-html插件生成html格式的测试报告文件

【忘了的话,来个pytest -h | findstr html,还可以输出为文本:pytest -h > pytest_help.txt】

1、格式:pytest -sv --html=测试报告的路径 要执行的文件

例如:pytest -sv --html=report.html xfail_test.py

会自动在当前目录生成html文件和assets目录,如下:

Python Pytest框架(五)生成测试报告_第4张图片

2、如果不想生成assets目录,格式:pytest -sv --html=测试报告的路径 --self-contained-html 要执行的文件


二、使用allure-pytest插件生成html格式的测试报告文件

Python Pytest框架(五)生成测试报告_第5张图片

1、pytest --alluredir=report(文件夹) xxx.py:执行结果打印在终端,同时生成report文件夹,里面是json格式的测试结果。

Python Pytest框架(五)生成测试报告_第6张图片

一直生成,就会一直叠加----->先清空目录,再生成测试结果:pytest --alluredir=report --clean-alluredir xxx.py

2、转成HTML格式的测试报告,需要安装工具(默认你已安装JDK):[懒癌假装放了链接]

解压,把D:\Allure\allure-2.13.0\bin添加到环境变量Path里。

然后,重启pycharm。

3、生成HTML格式的测试报告:

allure generate report(文件夹)----->默认存放在allure-report文件夹里

allure generate report -o test_report----->指定存放在report文件夹里

如果test_report文件夹已存在:

allure generate -c report -o test_report

4、网页打开

Python Pytest框架(五)生成测试报告_第7张图片

 

三、注意

1、结果是xpass的,在报告里显示为pass

2、结果是xfail的,在报告里显示为skipped

 

四、如何通过代码执行?如何通过装饰器详细化测试报告?

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

 创建框架入口

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('生成测试报告成功')

 

Python Pytest框架(五)生成测试报告_第8张图片

 

你可能感兴趣的:(Python Pytest框架(五)生成测试报告)