简介
Allure是一款非常轻量级并且非常灵活的开源测试报告生成框架。 Allure 是一个独立的报告插件,生成美观易读的报告,它支持绝大多数测试框架, 例如TestNG、Pytest、JUint等。它简单易用,易于集成。
Allure 安装
安装 python 插件
- 使用命令安装
pip3 install allure-pytest
- 下载源码安装 https://pypi.org/project/allure-pytest
安装 allure
- 下载: https://bintray.com/qameta/generic/allure2
- 前置条件:已部署java环境
- 配置环境变量:bin 目录配置到 path 系统环境变量
- 检查是都配置成功:cmd输入allure命令,测试是否安装成功
Allure使用
- pytest.ini 配置文件
[pytest]
# 添加行参数
addopts = -s --alluredir ./report/result
# 文件搜索路径
testpaths = ./scripts
# 文件名称
python_files = test_*.py
# 类名称
python_classes = Test*
# 方法名称
python_functions = test_*
- demo示例
@allure.feature("Allure测试标签")
class TestAllure:
def setup(self):
self.result = None
def teardown(self):
desc = "<font color='red'>实际结果:</br> </font>{}".format(self.result)
allure.dynamic.description(desc)
@allure.title("测试用例1")
@allure.description("测试用例1描述")
def test_01(self):
print('----------test1---------')
self.result = "test1"
@allure.title("测试用例2")
def test_02(self):
print('-----------test2-----------')
self.result = "test2"
@allure.title("测试用例3")
def test_03(self):
print('----------test3------------')
self.result = "test3"
if __name__ == '__main__':
pytest.main(['test_allure.py'])
- 运行结果,查看report/result目录
- 生成html格式,运行命令
allure generate report/result -o report/html --clean
- 生成在线测试报告: allure serve report/result
Allure 详解
title 标题
@allure.title("用例标题1")
def test_1():
pass
@allure.title("用例标题2")
def test_2():
pass
description 描述
@allure.title("用例标题1")
@allure.description("这里是对test_1用例的一些详细说明")
def test_1():
pass
@allure.title("用例标题2")
def test_2():
"""
test_2的描述
"""
pass
标签 @allure.feature
@allure.feature('这里是一级标签:test')
class TestAllure:
@allure.title("用例标题1")
@allure.description("这里是对test_1用例的一些详细说明")
def test_1(self):
pass
@allure.title("用例标题2")
def test_2(self):
pass
@allure.title("用例标题3")
def test_3(self):
pass
标签 @allure.story
@allure.feature('这里是一级标签:test')
class TestAllure:
@allure.title("用例标题1")
@allure.description("这里是对test_1用例的一些详细说明")
@allure.story("这里是二级标签:test_1")
def test_1(self):
pass
@allure.story("这里是二级标签:test_2")
@allure.title("用例标题2")
def test_2(self):
pass
@allure.story("这里是二级标签:test_3")
@allure.title("用例标题3")
def test_3(self):
pass
@allure.severity
定义用例的级别,有BLOCKER,CRITICAL,MINOR,NORMAL,TRIVIAL等几种类型,默认是NORMAL。
@allure.feature('这里是一级标签:test')
class TestAllure:
@allure.severity(allure.severity_level.BLOCKER)
@allure.title("用例标题1")
@allure.description("这里是对test_1用例的一些详细说明")
@allure.story("这里是二级标签:test_1")
def test_1(self):
pass
@allure.severity(allure.severity_level.CRITICAL)
@allure.story("这里是二级标签:test_2")
@allure.title("用例标题2")
def test_2(self):
pass
@allure.severity(allure.severity_level.NORMAL)
@allure.story("这里是二级标签:test_3")
@allure.title("用例标题3")
def test_3(self):
pass
只运行指定级别的用例
--allure_severities=critical,blocker
动态生成allure.dynamic
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报告自动生成
- 配置文件pytest.ini
addopts = -s --alluredir ./report/result
- 代码实现自动生成html报告
# 生成allure测试报告
Base.py
def allure_report(report_path, report_html):
# 执行命令 allure generate
allure_cmd = "allure generate %s -o %s --clean" % (report_path, report_html)
try:
subprocess.call(allure_cmd, shell=True)
my_log().debug("生成测试报告")
except:
my_log().debug("生成测试报告出错")
raise
设置程序主运行函数
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])
Base.allure_report(report_path, report_html_path)
Allure应用
- 代码实现
mport allure
import pytest
from pactverify.matchers import Matcher, EachLike, Like, PactVerify
from testcase.im.apis import ApiTest
@allure.feature("获取制定会话接口")
class TestGetConversation:
def setup(self):
self.result = None
self.verify_info = None
self.verify_result = None
self.request_param = None
def teardown(self):
desc = "<font color='red'>请求参数:</br> </font>{}</br>" \
"<font color='red'>实际结果:</br> </font>{}</br>" \
"<font color='red'>验证结果:</br> </font>{}</br>" \
"<font color='red'>验证信息:</br> </font>{}".format(self.request_param, self.result, self.verify_result,
self.verify_info)
allure.dynamic.description(desc)
# 获取制定会话接口
@pytest.mark.case_p0
@allure.title("根据conversationId获取制定会话接口")
def test_conversations_by_conversationId(self):
"""根据imId获取会话列表"""
conversationId = '5da9659ff53f07328308b355'
result = ApiTest().api_get_conversations_by_conversationId(conversationId)
print("返回的结果%s" % (result.json()))
expect_format = Matcher({
"statusCode": 200,
"status": "success",
"result": Like({
"unreadCount": Like(2, key_missable=True),
"messageReadTime": 34567890,
"userType": "CUSTOMER",
"_id": Matcher(conversationId),
"imId": "erp-2253F6D0-B558-466F-87FA-86395B5A2830",
"targetId": "erp-4B41582E-91D6-4337-846C-7D2B09D8F900",
"type": "PERSON",
"__v": 0,
"createdAt": "2019-10-18T07:11:27.350Z",
"displayName": "sdsa",
"latestMessage": {
"description": "hahahaha最近消息dhkasldhasjkfhklasfhklas"
},
"portraitUri": "https://cdn.mytoken.org/Fvuapzak9DwkHSuBPp0hcfw1emhf",
"tieId": "PERSON:erp-2253F6D0-B558-466F-87FA-86395B5A2830:erp-4B41582E-91D6-4337-846C-7D2B09D8F900",
"updatedAt": "2019-10-18T07:25:06.161Z",
"id": Matcher(conversationId)
})
})
mPactVerify = PactVerify(expect_format, hard_mode=True)
self.request_param = "conversationId" + str(conversationId)
self.result = result.json()
mPactVerify.verify(self.result)
self.verify_info = mPactVerify.verify_info
self.verify_result = mPactVerify.verify_result
assert mPactVerify.verify_result == True
-
测试报告
image.png
网友评论