一、场景
希望在报告中看到测试功能,子功能或场景、测试步骤、包括测试附加信息
二、解决
<font style="color:#EB2F96;">@Feature,@story,@step,@attach</font>
三、步骤
<font style="color:#F5222D;">import allure</font>
- 功能上加
<font style="color:#F5222D;">@allure.feature('功能名称')</font>
- 子功能上加
<font style="color:#F5222D;">@allure.store('子功能名称')</font>
- 步骤上加
<font style="color:#F5222D;">@allure.step('步骤细节')</font>
- 需要附加的信息
<font style="color:#F5222D;">@allure.attach('具体文本信息')</font>
- 如果只是测试登录功能运行的时候可以加显示
pytest 文件名 <font style="color:#F5222D;">--allure_features</font> '购物车功能' --allure-stories '加入购物车'
下划线
四、feature和store关系
- feature相当于一个功能,大模块、将case分类到某个feature中,报告中相当于testsuit
- store相当于对应这个功能或者模块下的不同场景,分支功能,属于feature之下的features中显示,相当于testcase
- 类似父子关系
五、@allure.step和with allure.step区别
- 测试过程中每个步骤,一般放在具体逻辑方法中
- 可以放在关键步骤中,在报告中显示
<font style="color:#F5222D;">@allure.step()</font>
只能以装饰器的形式放在类或者方法上面<font style="color:#F5222D;">with allure.step()</font>
可以放在测试用例方法里面,但测试步骤的代码需要被该语句包含
五、给测试用例划分优先级
场景
通常测试有PO、冒烟、验证上线测试,按重要级别来分别执行,比如上线跑一遍主流程和主要模块
解决
- 通过
<font style="color:#F5222D;">pytest.mark</font>
标记 - 通过
<font style="color:#F5222D;">allure.feature</font>``<font style="color:#F5222D;">allure.story</font>
- 也可以通过
<font style="color:#F5222D;">allure.severity</font>
来附加标记- 级别:
<font style="color:#F5222D;">Trivial</font>
不重要、<font style="color:#F5222D;">Minor</font>
不太重要、<font style="color:#F5222D;">Normal</font>
正常、<font style="color:#F5222D;">Critical</font>
严重、<font style="color:#F5222D;">Blocker</font>
阻塞
- 级别:
步骤
- 在方法、函数和类上面加
@allure.severity_level.TRIVIAL
@allure.severity(allure.severity_level.TRIVIAL)
def test_demo():
assert 1==1
- 执行时
pytest -s -v 文件名 --allure-severities normal,critical
Pytest+Allure生成测试报告
- 测试用例demo
testCy/testLearn.py
import pytest
import allure
@allure.feature('测试订单模块')
class TestOrder:
@allure.title('订单正例')
@allure.story('购买商品生成一条订单')
@allure.description('通过余额支付购买商品生成一个订单')
@allure.severity(allure.severity_level.TRIVIAL)
def test_payOrder_01(self):
with allure.step('调用商品详情接口获取itemd'):
print('调用商品详情接口')
with allure.step('调用提交订单接口生成待支付订单获取order_id'):
print('调用提交订单接口生成待支付订单获取order_id')
with allure.step('选择支付方式对订单完成支付'):
print('选择支付方式对订单完成支付')
@allure.title('订单反例')
@allure.story('余额不足生成订单失败')
@allure.description('余额不足生成待支付订单')
def test_payOrder_02(self):
with allure.step('调用商品详情接口获取itemd'):
print('调用商品详情接口')
with allure.step('调用提交订单接口生成待支付订单获取order_id'):
print('调用提交订单接口生成待支付订单获取order_id')
with allure.step('选择支付方式对订单完成支付'):
print('选择支付方式对订单完成支付')
- 执行命令文件
run.py
import os
import pytest
if __name__ == '__main__':
# 执行pytest命令,生成allure原始文件
pytest.main(['-vs','./testCy/testLearn.py','--alluredir=./reports/allure_row'])
# 先清空报告---》根据生成的原始文件生成allure-html报告到指定文职
make_report = "allure generate ./reports/allure_row -o ./reports/result --clean"
# 执行dos命令
os.system(make_report)
- 文件示例
allure_row
文件夹:执行完pytest命令之后生成的allure原始json文件result
文件夹:执行完dos命令生成的allure报告(美观html)