一、场景

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

二、解决

<font style="color:#EB2F96;">@Feature,@story,@step,@attach</font>

三、步骤

  1. <font style="color:#F5222D;">import allure</font>
  2. 功能上加<font style="color:#F5222D;">@allure.feature('功能名称')</font>
  3. 子功能上加<font style="color:#F5222D;">@allure.store('子功能名称')</font>
  4. 步骤上加<font style="color:#F5222D;">@allure.step('步骤细节')</font>
  5. 需要附加的信息<font style="color:#F5222D;">@allure.attach('具体文本信息')</font>
  6. 如果只是测试登录功能运行的时候可以加显示

pytest 文件名 <font style="color:#F5222D;">--allure_features</font> '购物车功能' --allure-stories '加入购物车' 下划线

四、feature和store关系

  1. feature相当于一个功能,大模块、将case分类到某个feature中,报告中相当于testsuit
  2. store相当于对应这个功能或者模块下的不同场景,分支功能,属于feature之下的features中显示,相当于testcase
  3. 类似父子关系

Allure - 图1

五、@allure.step和with allure.step区别

  1. 测试过程中每个步骤,一般放在具体逻辑方法中
  2. 可以放在关键步骤中,在报告中显示
  3. <font style="color:#F5222D;">@allure.step()</font>只能以装饰器的形式放在类或者方法上面
  4. <font style="color:#F5222D;">with allure.step()</font>可以放在测试用例方法里面,但测试步骤的代码需要被该语句包含

Allure - 图2

五、给测试用例划分优先级

场景

通常测试有PO、冒烟、验证上线测试,按重要级别来分别执行,比如上线跑一遍主流程和主要模块

解决

  1. 通过<font style="color:#F5222D;">pytest.mark</font>标记
  2. 通过<font style="color:#F5222D;">allure.feature</font>``<font style="color:#F5222D;">allure.story</font>
  3. 也可以通过<font style="color:#F5222D;">allure.severity</font>来附加标记
    1. 级别:<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>阻塞

步骤

  1. 在方法、函数和类上面加@allure.severity_level.TRIVIAL
  1. @allure.severity(allure.severity_level.TRIVIAL)
  2. def test_demo():
  3. assert 1==1
  1. 执行时
  1. pytest -s -v 文件名 --allure-severities normal,critical

Pytest+Allure生成测试报告

  1. 测试用例demo
  1. testCy/testLearn.py
  2. import pytest
  3. import allure
  4. @allure.feature('测试订单模块')
  5. class TestOrder:
  6. @allure.title('订单正例')
  7. @allure.story('购买商品生成一条订单')
  8. @allure.description('通过余额支付购买商品生成一个订单')
  9. @allure.severity(allure.severity_level.TRIVIAL)
  10. def test_payOrder_01(self):
  11. with allure.step('调用商品详情接口获取itemd'):
  12. print('调用商品详情接口')
  13. with allure.step('调用提交订单接口生成待支付订单获取order_id'):
  14. print('调用提交订单接口生成待支付订单获取order_id')
  15. with allure.step('选择支付方式对订单完成支付'):
  16. print('选择支付方式对订单完成支付')
  17. @allure.title('订单反例')
  18. @allure.story('余额不足生成订单失败')
  19. @allure.description('余额不足生成待支付订单')
  20. def test_payOrder_02(self):
  21. with allure.step('调用商品详情接口获取itemd'):
  22. print('调用商品详情接口')
  23. with allure.step('调用提交订单接口生成待支付订单获取order_id'):
  24. print('调用提交订单接口生成待支付订单获取order_id')
  25. with allure.step('选择支付方式对订单完成支付'):
  26. print('选择支付方式对订单完成支付')
  1. 执行命令文件
  1. run.py
  2. import os
  3. import pytest
  4. if __name__ == '__main__':
  5. # 执行pytest命令,生成allure原始文件
  6. pytest.main(['-vs','./testCy/testLearn.py','--alluredir=./reports/allure_row'])
  7. # 先清空报告---》根据生成的原始文件生成allure-html报告到指定文职
  8. make_report = "allure generate ./reports/allure_row -o ./reports/result --clean"
  9. # 执行dos命令
  10. os.system(make_report)
  1. 文件示例
    1. allure_row文件夹:执行完pytest命令之后生成的allure原始json文件
    2. result文件夹:执行完dos命令生成的allure报告(美观html)

Allure - 图3

Allure - 图4