Allure是一款非常轻量级并且非常灵活的开源测试报告生成框架。 Allure 是一个独立的报告插件,生成美观易读的报 告,它支持绝大多数测试框架, 例如TestNG、Pytest、JUint等。它简单易用,易于集成。
官网:http://allure.qatools.ru
帮助文档:https://docs.qameta.io/allure/
pytest.ini
[pytest]
# 添加行参数
addopts = -s --alluredir ./report/result
# 文件搜索路径
testpaths = ./scripts
# 文件名称 python_files = test_*.py
# 类名称
python_classes = Test*
# 方法名称 python_functions = test_*
allure_test1.py
import pytest
@pytest.fixture()
def sample():
print(‘打印输出sample‘)
def test_01(sample):
print(‘test1‘)
def test_02():
print(‘test2----‘)
def test_03(sample):
print(‘test3----‘)
if __name__ == ‘__main__‘:
pytest.main([‘allure_test1.py‘])
运行结果,查看report/result目录
生成html,运行命令: $ allure generate report/result -o report/html --clean
使用火狐浏览器访问index.html,不要使用谷歌(谷歌是loading)
生成在线 allure serve report/result
可以自定义用例标题,标题默认为函数名。@allure.title
import allure
import pytest
@allure.title("用例标题0")
def test_0():
pass
@allure.title("用例标题1")
def test_1():
pass
可以添加测试的详细说明
@allure.title("用例标题0")
@allure.description("这里是对test_0用例的一些详细说明")
def test_0():
pass
@allure.title("用例标题1")
def test_1():
"""
test_1的描述
"""
pass
@allure.title("用例标题2")
def test_2():
pass
@allure.feature(‘这里是一级标签:test‘)
class TestAllure:
@allure.title("用例标题0")
@allure.description("这里是对test_0用例的一些详细说明")
def test_0(self):
pass
@allure.title("用例标题1")
def test_1(self):
pass
@allure.title("用例标题2")
def test_2(self):
pass
@allure.feature(‘这里是一级标签:test‘)
class TestAllure:
@allure.title("用例标题0")
@allure.description("这里是对test_0用例的一些详细说明")
@allure.story("这里是二级标签:test_0")
def test_0(self):
pass
@allure.story("这里是二级标签:test_1")
@allure.title("用例标题1")
def test_1(self):
pass
@allure.story("这里是二级标签:test_2")
@allure.title("用例标题2")
def test_2(self):
pass
定义用例的级别,Graphs主要有BLOCKER,CRITICAL,MINOR,NORMAL,TRIVIAL等几种类型,默认是NORMAL。
@allure.feature(‘这里是一级标签:test‘)
class TestAllure:
@allure.severity(allure.severity_level.BLOCKER)
@allure.title("用例标题0")
@allure.description("这里是对test_0用例的一些详细说明")
@allure.story("这里是二级标签:test_0")
def test_0(self):
pass
@allure.severity(allure.severity_level.CRITICAL)
@allure.story("这里是二级标签:test_1")
@allure.title("用例标题1")
def test_1(self):
pass
@allure.severity(allure.severity_level.NORMAL)
@allure.story("这里是二级标签:test_2")
@allure.title("用例标题2")
def test_2(self):
pass
只运行指定级别的用例 --allure_severities=critical,blocker
可以使用函数体内的数据动态生成
params_1={"name":"动态获取test1","method":"post","url":"http://www.baidu.com"}
params_2={"name":"动态获取test2","method":"get","url":"http://www.baidu.com"}
@allure.title("用例标题0")
@allure.severity(severity_level=allure.severity_level.CRITICAL)
def test_0():
pass
@allure.title("用例标题1")
def test_1():
pass
@pytest.mark.parametrize("params",[params_1,params_2])
def test_2(params):
allure.dynamic.title(params["name"])
#allure
#sheet名称 feature 一级标签
allure.dynamic.feature(sheet_name)
#模块 story 二级标签
allure.dynamic.story(case_model)
#用例ID+接口名称 title
allure.dynamic.title(case_id+case_name)
#请求URL 请求类型 期望结果 实际结果描述
增加生成allure属性 addopts = -s --alluredir ./report/result
代码实现自动生成html报告
import os import subprocess def init_report(): cmd = "allure generate report/result -o report/html --clean" subprocess.call(cmd, shell=True) res_path= os.path.abspath(os.path.dirname(__file__)) report_path = res_path + "/report/" + "index.html"
run.py
if __name__ == ‘__main__‘: report_path = Conf.get_report_path()+os.sep+"result" report_html_path = Conf.get_report_path()+os.sep+"html" pytest.main(["-s","--alluredir",report_path])
原文:https://www.cnblogs.com/siguadd/p/14835644.html