pytest+allure
创始人
2024-12-16 10:05:49
0

安装

下载:github win环境下载zip

环境变量:

pycharm:

        pip install allure-pytest

验证安装

生成结果:

 if __name__ == '__main__':     pytest.main(['-s','test_createTag2.py','--alluredir','result'])

生成报告:

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

完整: 

if __name__ == '__main__':     pytest.main(['-s','test_createTag2.py','--alluredir','./result'])     os.system('allure generate ./result -c -o ./report')

添加装饰器/方法后才能在报告中显示相关结果

12、用法(二)之生成测试报告_哔哩哔哩_bilibili

使用装饰器

  1. @allure.feature@allure.story: 这些装饰器用于描述测试的功能和具体的故事或场景。

    1import allure 2 3@allure.feature('User Management') 4@allure.story('Login') 5def test_valid_login(): 6    # 测试代码
  2. @allure.severity: 设置测试案例的严重性等级。

    1@allure.severity(allure.severity_level.CRITICAL) 2def test_critical_function(): 3    # 测试代码
  3. @allure.issue@allure.tms_link: 链接到问题跟踪系统中的问题或测试管理系统中的测试用例。

    1@allure.issue('PROJECT-123') 2@allure.tms_link('TC-1234') 3def test_linked_case(): 4    # 测试代码

在测试函数内使用allure方法

  1. allure.attach(): 添加文本、图片、HTML或任何类型的附件到测试报告。

    1def test_attach(): 2    allure.attach('This is a text attachment', attachment_type=allure.attachment_type.TEXT) 3    # 测试代码
  2. allure.step(): 创建可折叠的步骤,使测试报告更清晰。

    1def test_with_steps(): 2    with allure.step('Step 1'): 3        # 执行步骤1 4    with allure.step('Step 2'): 5        # 执行步骤2

Environment设置

  • Environment可以理解为环境变量;

  • 默认为空;

  • 可以自己设置。

   

创建environment.xml

    Browser  Chrome      Browser.Version  111.0.5563.65      Env  Test      IP  192.168.1.133      Allure-Pytest.Version  2.8.12     

  

Categories设置

1.Categories即分类,

测试用例结果的分类

默认有两种分类:

# Product defects 产品缺陷(测试结果:failed)

# Test defects 测试缺陷(测试结果:error/broken)

可以自定义分类。

2. 设置方式

创建categories.json文件

[  {  "name": "Passed tests",  "matchedStatuses": ["passed"]  },  {  "name": "Ignored tests",  "matchedStatuses": ["skipped"]  },  {  "name": "Infrastructure problems",  "matchedStatuses": ["broken", "failed"],  "messageRegex": ".*bye-bye.*"  },  {  "name": "Outdated tests",  "matchedStatuses": ["broken"],  "traceRegex": ".*FileNotFoundException.*"  },  {  "name": "Product defects",  "matchedStatuses": ["failed"]  },  {  "name": "Test defects",  "matchedStatuses": ["broken"]  }  ] 

  参数说明:

  • name:分类名称
  • matchedStatuses:测试用例的运行状态,默认["failed", "broken", "passed", "skipped", "unknown"]
  • messageRegex:测试用例运行的错误信息,默认.* ,通过正则匹配
  • traceRegex:测试用例运行的错误堆栈信息,默认.* ,通过正则匹配

  

@pytest.mark.parametrize()

Allure能够很好的支持@pytest.mark.parametrize()进行参数化

  

with allure.step

@allure.step()标记函数使之成为测试步骤,而在测试函数/方法中,我们还可以通过with allure.step()的方式标记测试步骤。

它们之间的区别在于,@allure.step()用于标记通用函数,当这个被标记的函数被调用后,会插入步骤说明并展示在Allure报告中。

而with allure.step()则是将普通的代码标记为测试步骤,执行到这段代码时则会在Allure报告中展示步骤说明。

我们在上面代码的基础上,加入with allure.step(),示例如下:

import pytest import allure import os   class Counter:     def __init__(self):         self.value = 0      def increment(self):         self.value += 1      @allure.step("get value")     def get_value(self):         return self.value   @pytest.fixture def counter(request):#request 参数不需要传值     #setup     with allure.step("with.allure.step"):         c = Counter()         print("setup_value:",c.value)      def reset_counter():         #还原数据         c.value = 0         print("teardown_value:",c.value)      request.addfinalizer(reset_counter)      return c   def test_counter(counter):     print('test-start')     assert counter.get_value() == 0     counter.increment()     assert counter.get_value() == 1     counter.increment()     assert counter.get_value() == 2  if __name__ == '__main__':     pytest.main(['-s','test_yield_finalizer.py','--alluredir','./result'])     os.system('allure generate ./result -c -o ./report')

fixture

pytest的fixture函数可以实现setup、teardown的功能,而Allure会跟踪每个fixture的调用情况,详细显示调用了哪些fixture和参数以及调用顺序。

  

@pytest.mark.skip(reason='这个是跳过的原因!')

@pytest.mark.skipif(condition, reason='跳过的原因!')

@pytest.skip()在用例内部跳过测试用例

@pytest.mark.xfail(condition, reason=‘理由!’)

tags

其他:

https://blog.csdn.net/2301_81967703/article/details/138576981

allure.attach


allure.attach用于在测试报告中添加附件,补充测试结果。附件格式可以是txt、jpg等,附件内容通常是测试数据、截图等。

allure.attach提供了两种方法:allure.attach(),allure.attach.file()

allure.attach()
作用:在测试报告中生成指定内容、名称、类型的附件

语法:allure.attach(body, name=None, attachment_type=None, extension=None)

参数说明:

body,需要显示的内容,也可以理解为写入附件的内容
name,附件名称
attachment_type,附件类型,如csv、jpg、html 等,由allure.attachment_type提供
extension:附件扩展名,不常用
allure.attach.file()
作用:向测试用例中上传附件

语法:allure.attach.file(source, name=None, attachment_type=None, extension=None)

参数说明:source为文件路径,其他参数与allure.attach()参数一致。

在UI自动化测试中,会经常用到这个方法来上传用例执行的截图。
 

Note:

关于Flaky test 

相关内容

热门资讯

据统计"拱趴大菠萝9... 据统计"拱趴大菠萝9人场技巧"总是是有辅助工具(哔哩哔哩)1、每一步都需要思考,不同水平的挑战会更加...
玩家必备攻略!微信小程序免费黑... 玩家必备攻略!微信小程序免费黑科技(外挂),微信小程序微乐房间有挂吗其实有挂辅助破解侠是真的助透视。...
据报道!阿当比鸡有透视吗,玉兔... 据报道!阿当比鸡有透视吗,玉兔辅助器(竟然是真的器)-哔哩哔哩1、玩家可以在阿当比鸡有透视吗线上大神...
有玩家发现!wpk系统是否存在... 有玩家发现!wpk系统是否存在作必弊行为,凑一桌游戏软件下载辅助,切实真的是有辅助插件(哔哩哔哩)1...
方法辅助挂"中至赣州... 方法辅助挂"中至赣州冲关小程序辅助"果然真的是有辅助方法(哔哩哔哩)1、用户打开应用后不用登录就可以...
推荐一款!!微信小程序免费黑科... 推荐一款!!微信小程序免费黑科技(外挂),微乐辅助和靠谱吗真是今日头条1)有没有挂:进一步探索有辅助...
截至目前!决战十水三修改器,微... 截至目前!决战十水三修改器,微乐小程序微乐房间怎么开挂(都是是有安装)-哔哩哔哩1、操作简单,无需微...
据监测!wepoker轻量版有... 据监测!wepoker轻量版有透视吗,新道游辅助软件下载,总是真的有辅助插件(哔哩哔哩)1、超多福利...
2026版教程"神兽... 2026版教程"神兽h5平台反杀"原来是真的有辅助插件(哔哩哔哩)1、超多福利:超高返利,海量正版游...
重大推荐!微信小程序免费黑科技... 重大推荐!微信小程序免费黑科技(外挂),微乐家乡小程序脚本ios好像确实有挂1、有没有辅助教程、有透...