Allure安装使用

安装 Allure

提前安装配置好 JDK

windows

mac

brew install allure

在 pytest 中使用

1、安装allure-pytest插件:
pip install allure-pytest  (安装allure生成测试报告)
2、运行:
  • 在测试执行期间收集结果:
pytest --alluredir=./myAllureReport ( 指定 allure 报告数据生成路径 )
3、查看测试报告:

** 方法1:**

pytest serve ./myAllureReport ( 使用 myAllureReport 下面的数据生成在线 html 报告,会直接打开在线报告 )

** 方法2:**

allure generate ./myAllureReport/ -o ./report --clean  ( 使用 myAllureReport 文件下面的数据生成报告放在 report 下 )
allure open -h 127.0.0.1 -p 3333 ./report  (启动本地服务生成链接查看报告)

效果图(亲试有效)

Allure报告优化

类上面添加项目名、模块名、标题

1、场景:
希望在报告中看到测试功能,子功能或场景,测试步骤,包括测试附加信息

2、解决:
使用 @Feature,@story,@step,@attach

3、步骤:
import allure
@allure.epic("项目1")
功能上加 @allure.feature('功能名称')
@allure.title("成功的用例")
子功能上加@allure.story('子功能名称')
步骤上加@allure.step('步骤细节')
@allure.attach('具体文本信息),需要附加的信息,可以是数据,文本,图片,视频,网页
如果只测试登录功能运行的时候可以加限制过滤:
pytest [文件名] --allure_features '购物车功能' --allure-stories '加入购物车' (注意这里—allure_features中间是下划线)

4、实例test_feature_story.py代码如下

import pytest
import allure

# 标识整个测试类都是用来进行订单操作
@allure.epic("项目1")
@allure.feature('创建订单')
class Test_Ordering:
    @pytest.mark.hello
    @pytest.mark.login
    @pytest.mark.Fail_retry
    @allure.story('创建订单成功')
    @allure.title("成功得用例")
    def test_c(self):
        print("查询订单")
        raise Exception("失败测试用例重试1")

    @allure.story("创建订单失败")
    def test_a(self):
        print("这是一个测试失败重试10次的测试用例")


@allure.feature('查询订单')
class Test_GetOrdering:
    @pytest.mark.hello
    @pytest.mark.login
    @pytest.mark.Fail_retry
    @allure.story('查询订单失败')
    def test_003(self):
        print("查询订单失败")
        raise Exception("失败测试用例重试1")

    @allure.story('查询订单成功')
    def test_002(self):
        print("这是一个测试失败重试10次的测试用例")

@allure.feature('失败重试')
class Test_Rerunfailure:

    @allure.story('失败重试1')
    def test_001(self):
        print("这是一个测试通过的测试用例")
        with allure.step("重试用例1"):
            raise Exception("失败测试用例重试1")

    @allure.story('失败重试2')
    @pytest.mark.login
    def test_002(self):
        print("这是一个测试失败重试3次的测试用例")
        with allure.step("重试用例2"):
            raise Exception("失败测试用例重试2")

在控制台执行

pytest --alluredir=./myAllureReport
allure generate ./myAllureReport/ -o ./report --clean   ( 使用 myAllureReport 文件下面的数据生成报告放在 report 下 )
allure open -h 127.0.0.1 -p 3333 ./report  (启动本地服务生成链接查看报告)

  • 测试步骤

将生成 allure 测试报告的命令集成到 main.py 文件

import pytest
import time
import os


if __name__ == '__main__':
    pytest.main(["-vs"])    # 调用pytest的main函数执行测试
    times = time.strftime("%Y_%m_%d_%H_%M_%S",time.localtime())
    os.system('allure generate ./myAllureReport -o ./report/report_'+times+' --clean')    // cmd 执行 生成 Allure 测试报告文件
    os.system(r'@echo y | del .\myAllureReport\*')  // cmd 删除测试数据文件,并自动输入 y 回车
    os.system(r'allure open -h 127.0.0.1 -p 3333 ./report/report_'+times)    // // cmd 执行开启本地 Allure 服务并打开测试报告

具体参考:
1、https://www.jianshu.com/p/0042fc38abbc
2、https://www.cnblogs.com/miracle-wei/articles/15592425.html
3、https://blog.csdn.net/qq_35451939/article/details/111274752 修改logo及标题

posted @ 2022-04-28 09:46  DeyouKong  阅读(1008)  评论(0编辑  收藏  举报