python + pytest + allure 生成测试报告

一、环境准备

1、下载 allure 地址:

方式 1: git下载地址https://github.com/allure-framework/allure2/releases

找到自己想要的版本下载

方式 2:下载地址:Central Repository: io/qameta/allure/allure-commandline/2.17.3https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/2.17.3/
选择一个版本:

2、配置 allure 环境变量

解压下载的 allure 文件,找到 bin 文件目录路径

mac:

        将路径放到 .bash_profile 中,如下:

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

        或者:

export /Users/XXX/allure-2.17.3/bin

        然后:

source ~/.bash_profile

        Windows:配置到环境变量中即可

        然后重启 pycharm 即可

3、 allure 运行需要 jdk 支持,所以记得安装 jdk

        网上自己下载安装即可

4、安装 allure-pytest 插件

        可以通过 pycharm 安装

        也可以pip install allure-pytest 安装


二、

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

1、pytest --alluredir=report(文件夹) xxx.py

执行结果打印在终端,同时生成report文件夹,里面是json格式的测试结果。

一直生成,就会一直叠加----->先清空目录,再生成测试结果:

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

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

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

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

如果test_report文件夹已存在:

allure generate -c report -o test_report


三、如何通过代码执行,如何通过装饰器细化测试报告

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、创建框架入口

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


 

四、结果展示

五、allure 的部分功能

标记测试点:@allure.feature    可以用来定制测试类标题,如:登录

标记测试用例:@allure.story     可以用来定制函数方法标题,如:登录成功

测试用例-参数化/数据驱动:@pytest.mark.parametrise

测试步骤:@allure.step

@pytest.allure.step:可以用来给函数方法定义调用step名称。po调用-步骤描述。(推荐:po分层设计,调用函数方法使用装饰器指定step名称)

with allure.step:在指定位置记录step,with包含的语句块为step应该执行的操作。

allure.attach:标注增加附件,如:截图。allure.attach(name,body,type)

@allure.issue("url"):关联bug,如:禅道xxbug

@allure.testcase("url"):关联case,如:禅道xxcase
 

猜你喜欢

转载自blog.csdn.net/Jiazengzeng/article/details/123074297