笔记记录-pytest+allure定制报告
上篇基础:pytest高级操作
测试用例一般采用数据驱动的方式,高级点采用关键字与数据驱动结合,外加一些私人定制。
首先僚机额allure参数配置,一般采用结合的方式进行定制,单一模块无法满足功能需求。
参考:文章
第一种方法:写在函数里面(推荐),这种方法的好处是可以把测试用例完全分离出去,title,severity等方法直接从用例中获取,可操作性爆表;缺点是测试用例重复性比较多
@pytest.mark.parametrize('tid, describe, modules, video_file,args', data) # 用例模块
@pytest.mark.flaky(reruns=1) # 失败用例重跑
@allure.step("第一步:拆分执行")
def test_001(index):
allure.dynamic.title(title)# 可循环获取testdata中的值,实现变量传参
allure.dynamic.feature('功能模块')
allure.dynamic.story('功能名称')
allure.dynamic.description('测试用例描述')
allure.dynamic.severity(severity)# 可循环获取testdata中的值,实现变量传参
allure.dynamic.tag('p0')
第二种方法:作为装饰器(看需求选择),好处是显着逼格很高,对每一个模块统一命名,测试用例中某些重复的比较少;缺点是没办法对每一个测试用例进行级别控制,并在allure报告中显示出来
@allure.feature('通用基础业务支持')
@allure.story('1000: 请求建立连接')
@allure.severity('critical')# 不能循环从testdata中取参
@allure.title('{title}')# 可循环获取testdata中的值,实现变量传参
@pytest.mark.parametrize('index,title', list(range(len(testdata))))
def test_001(index,tilte):
pass
执行生成测试报告
import pytest
import sys, os
import shutil
dir_path = os.path.dirname(os.path.dirname(__file__))
sys.path.append(dir_path)
Basepath = os.path.split(os.path.dirname(os.path.abspath(__file__)))[0]
file_name = Basepath + "\\case"
if __name__ == "__main__":
# pytest.main(['-vs'])
# unittest.main()
filename = os.path.dirname(os.path.dirname(__file__))
copy_file = os.path.join(filename, 'config', 'environment.properties')
paste_file = os.path.join(filename, 'result', 'environment.properties')
# shutil.copy(copy_file.replace('\\', '/'), paste_file.replace('\\', '/')) # 替换
test_dir = os.path.dirname(__file__)
pytest.main(['-vs', '-q', file_name, '--alluredir', '../result/', "--clean-alluredir"])
shutil.copy(copy_file, paste_file) # 复制生成环境变量
os.system('allure generate ../result/ -o ../report/ --clean')
# os.system('allure open -h 127.0.0.1 -p 8881 ../report/') # 运行完,自动打开测试报告