准备条件:
安装 Allure 并配置环境变量 https://github.com/allure-framework/allure2/releases
import allure
报告生成:
结合pytest框架生成allure报告
if __name__ == '__main__':
import pytest
import os
pytest.main(['-vs', './test_case_api/test.py', '--alluredir=../tmp/allure-results', '../tmp/allure-results'])
os.system("allure generate ../tmp/allure-results -o ../tmp/html --clean")
生成json文件的固定写法:
pytest.main(['-vs',用例脚本文件路径,指定alluredir路径,生成json报告存放路径])
输出json文件为html文件固定写法:
os.system("allure generate json文件存放路径 -o html文件存放路径 --clean")
函数标记装饰器
函数标记装饰器,可以同步展示到测试报告内(函数才能使用)
@allure.epic 史诗->系统
@allure.feature 模块
@allure.story 故事
@allure.title 标题
@allure.step 步骤
@allure.feature("xx模块")
class Test_100(unittest.TestCase):
@allure.story("用户中心")
@allure.title('新增用户')
def test_01(self):
with allure.step("新增"):
生成报告效果如下:
1、在展示层级上,epic包含feature,feature包含story,story包含title,title包含step
2、它们可以同时使用,也可以单独使用,也可以不使用
标记方式都支持汉字
with allure.step()在python脚本中都可以使用,是代码块形式
with allure.step():
print("第一步")
def wait(self, assert_sql, value):
assert_sql = Template(assert_sql).substitute(goods=Argument.goods)
sql_connect = Sql()
result = sql_connect.execute(assert_sql).fetchone()
with allure.step("当状态为空时,等待3s"):
while not result:
result = sql_connect.execute(assert_sql).fetchone()
time.sleep(3)
with allure.step("当状态不为空时,判断状态值"):
while result:
time.sleep(3)
result = sql_connect.execute(assert_sql).fetchone()
if result[0] == int(value):
with allure.step("成功"):
break
else:
with allure.step("循环等待3s"):
continue