当前位置:网站首页>Allure进阶-动态生成报告内容
Allure进阶-动态生成报告内容
2022-07-30 13:49:00 【司小幽】
目录
1.参数化结合allure.title()生成不同标题报告
1.参数化parametrize
import os
import pytest
# # 测试数据
# test_datas = [
# #"username":"zz1","password":"123456" 是用例的输入数据
# # "success!" 是预期结果
# ({"username":"zz","password":"111111"},"success!"),
# ({"username":"xz","password":"222222"},"failed!"),
# ({"username":"qs","password":"333333"},"success!")
# ]
# pytest用例函数
# @pytest.mark.parametrize("a,b",[('zz','123456'),('xz','123456'),('qs','123456')])
# def test_login(a,b):
# """测试登录用例"""
# print("登录")
# print(a)
# print(b)
# @pytest.mark.parametrize("test_input,expected",test_datas)
# def test_login(test_input,expected):
# """测试登录用例"""
# print("登录")
# # 获取输入数据
# print("获取输入数据")
# print(test_input["username"])
# print(test_input["password"])
# # 获取预期结果
# print("获取预期结果")
# print(expected)
# 模拟定义登录接口
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
# 返回
return {
"code":0,"msg":"success!"}
# # 测试数据
test_datas = [
#"username":"zz1","password":"123456" 是用例的输入数据
# "success!" 是预期结果
({
"username":"zz","password":"111111"},"success!"),
({
"username":"xz","password":"222222"},"failed!"),
({
"username":"qs","password":"333333"},"success!")
]
@pytest.mark.parametrize("test_input,expected",test_datas)
def test_login(test_input,expected):
"""测试登录用例"""
# 获取函数返回结果
result = login(test_input["username"],test_input["password"])
# 断言
assert result["msg"] == expected
if __name__ == '__main__':
# 生成报告数据源到result目录,存储json数据
pytest.main(['test_a_01.py','--alluredir','./result','--clean-alluredir'])
# 基于json数据生成web报告工程
os.system('allure generate ./result -o ./report_allure/ --clean')
2.param+ids参数
import os
import pytest
# # 测试数据
# test_datas = [
# #"username":"zz1","password":"123456" 是用例的输入数据
# # "success!" 是预期结果
# ({"username":"zz","password":"111111"},"success!"),
# ({"username":"xz","password":"222222"},"failed!"),
# ({"username":"qs","password":"333333"},"success!")
# ]
# pytest用例函数
# @pytest.mark.parametrize("a,b",[('zz','123456'),('xz','123456'),('qs','123456')])
# def test_login(a,b):
# """测试登录用例"""
# print("登录")
# print(a)
# print(b)
# @pytest.mark.parametrize("test_input,expected",test_datas)
# def test_login(test_input,expected):
# """测试登录用例"""
# print("登录")
# # 获取输入数据
# print("获取输入数据")
# print(test_input["username"])
# print(test_input["password"])
# # 获取预期结果
# print("获取预期结果")
# print(expected)
# 模拟定义登录接口
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
# 返回
return {
"code":0,"msg":"success!"}
# # 测试数据
test_datas = [
#"username":"zz1","password":"123456" 是用例的输入数据
# "success!" 是预期结果
({
"username":"zz","password":"111111"},"success!"),
({
"username":"xz","password":"222222"},"failed!"),
({
"username":"qs","password":"333333"},"success!")
]
@pytest.mark.parametrize("test_input,expected",test_datas,
ids=["输入正确的账号A,密码,登录",
"输入错误的账号B,密码,登录",
"输入正确的账号A,密码,登录"
]
)
def test_login(test_input,expected):
"""测试登录用例"""
# 获取函数返回结果
result = login(test_input["username"],test_input["password"])
# 断言
assert result["msg"] == expected
if __name__ == '__main__':
# 生成报告数据源到result目录,存储json数据
pytest.main(['test_a_ids_02.py','--alluredir','./result2','--clean-alluredir'])
# 基于json数据生成web报告工程
os.system('allure generate ./result2 -o ./report_allure2/ --clean')
3.allure.title描述用例
import os
import allure
import pytest
# # 测试数据
# test_datas = [
# #"username":"zz1","password":"123456" 是用例的输入数据
# # "success!" 是预期结果
# ({"username":"zz","password":"111111"},"success!"),
# ({"username":"xz","password":"222222"},"failed!"),
# ({"username":"qs","password":"333333"},"success!")
# ]
# pytest用例函数
# @pytest.mark.parametrize("a,b",[('zz','123456'),('xz','123456'),('qs','123456')])
# def test_login(a,b):
# """测试登录用例"""
# print("登录")
# print(a)
# print(b)
# @pytest.mark.parametrize("test_input,expected",test_datas)
# def test_login(test_input,expected):
# """测试登录用例"""
# print("登录")
# # 获取输入数据
# print("获取输入数据")
# print(test_input["username"])
# print(test_input["password"])
# # 获取预期结果
# print("获取预期结果")
# print(expected)
# 模拟定义登录接口
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
# 返回
return {
"code":0,"msg":"success!"}
# # 测试数据
test_datas = [
#"username":"zz1","password":"123456" 是用例的输入数据
# "success!" 是预期结果
({
"username":"zz","password":"111111"},"success!"),
({
"username":"xz","password":"222222"},"failed!"),
({
"username":"qs","password":"333333"},"success!")
]
@allure.title("用例描述,测试输入:{test_input}")
@pytest.mark.parametrize("test_input,expected",test_datas)
def test_login(test_input,expected):
"""测试登录用例"""
# 获取函数返回结果
result = login(test_input["username"],test_input["password"])
# 断言
assert result["msg"] == expected
if __name__ == '__main__':
# 生成报告数据源到result目录,存储json数据
pytest.main(['test_a_title_03.py','--alluredir','./result3','--clean-alluredir'])
# 基于json数据生成web报告工程
os.system('allure generate ./result3 -o ./report_allure3/ --clean')
4.优化用例title
import os
import allure
import pytest
# # 测试数据
# test_datas = [
# #"username":"zz1","password":"123456" 是用例的输入数据
# # "success!" 是预期结果
# ({"username":"zz","password":"111111"},"success!"),
# ({"username":"xz","password":"222222"},"failed!"),
# ({"username":"qs","password":"333333"},"success!")
# ]
# pytest用例函数
# @pytest.mark.parametrize("a,b",[('zz','123456'),('xz','123456'),('qs','123456')])
# def test_login(a,b):
# """测试登录用例"""
# print("登录")
# print(a)
# print(b)
# @pytest.mark.parametrize("test_input,expected",test_datas)
# def test_login(test_input,expected):
# """测试登录用例"""
# print("登录")
# # 获取输入数据
# print("获取输入数据")
# print(test_input["username"])
# print(test_input["password"])
# # 获取预期结果
# print("获取预期结果")
# print(expected)
# 模拟定义登录接口
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
# 返回
return {
"code":0,"msg":"success!"}
# # 测试数据
test_datas = [
#"username":"zz1","password":"123456" 是用例的输入数据
# "success!" 是预期结果
({
"username":"zz","password":"111111"},"success!","输入正确的账号,密码登录"),
({
"username":"xz","password":"222222"},"failed!","输入错误的账号,密码登录"),
({
"username":"qs","password":"333333"},"success!","输入正确的账号,密码登录")
]
@allure.title("{title}{test_input}")
@pytest.mark.parametrize("test_input,expected,title",test_datas)
def test_login(test_input,expected,title):
"""测试登录用例"""
# 获取函数返回结果
result = login(test_input["username"],test_input["password"])
# 断言
assert result["msg"] == expected
if __name__ == '__main__':
# 生成报告数据源到result目录,存储json数据
pytest.main(['test_a_title_new_04.py','--alluredir','./result4','--clean-alluredir'])
# 基于json数据生成web报告工程
os.system('allure generate ./result4 -o ./report_allure4/ --clean')
2.allure报告清空上一次运行的记录
import os
import pytest
# # 测试数据
# test_datas = [
# #"username":"zz1","password":"123456" 是用例的输入数据
# # "success!" 是预期结果
# ({"username":"zz","password":"111111"},"success!"),
# ({"username":"xz","password":"222222"},"failed!"),
# ({"username":"qs","password":"333333"},"success!")
# ]
# pytest用例函数
# @pytest.mark.parametrize("a,b",[('zz','123456'),('xz','123456'),('qs','123456')])
# def test_login(a,b):
# """测试登录用例"""
# print("登录")
# print(a)
# print(b)
# @pytest.mark.parametrize("test_input,expected",test_datas)
# def test_login(test_input,expected):
# """测试登录用例"""
# print("登录")
# # 获取输入数据
# print("获取输入数据")
# print(test_input["username"])
# print(test_input["password"])
# # 获取预期结果
# print("获取预期结果")
# print(expected)
# 模拟定义登录接口
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
# 返回
return {
"code":0,"msg":"success!"}
# # 测试数据
test_datas = [
#"username":"zz1","password":"123456" 是用例的输入数据
# "success!" 是预期结果
({
"username":"zz","password":"111111"},"success!"),
({
"username":"xz","password":"222222"},"failed!"),
({
"username":"qs","password":"333333"},"success!")
]
#
# @pytest.mark.parametrize("test_input,expected",test_datas)
# def test_login(test_input,expected):
# """测试登录用例"""
# # 获取函数返回结果
# result = login(test_input["username"],test_input["password"])
# # 断言
# assert result["msg"] == expected
def test_case02():
assert 1 == 1
if __name__ == '__main__':
# 1.allure报告可以记录用例每次执行的情况,方便跟踪用例的成功率,数据保留在json文件中
# 2.会带来一个问题,当你代码里的用例删除或者更换名称后,依然会记录之前的用例报告
# 3. --clean-alluredir 每次用例执行之前先清空allure的报告记录
pytest.main(['test_a_05.py','--alluredir','./result','--clean-alluredir'])
# 基于json数据生成web报告工程
# 4.这里的clean只是让报告可重新生成,生成的结果,会保留之前的用例执行记录
os.system('allure generate ./result -o ./report_allure/ --clean')
3.allure动态生成用例标题
import os
import allure
# 接口用例描述例子
import pytest
desc = "<font color = 'red'>请求URL:</font>{}<Br/>" \
"<font color = 'red'>请求类型L:</font>{}<Br/>" \
"<font color = 'red'>期望结果:</font>{}<Br/>" \
"<font color = 'red'>实际结果:</font>{}<Br/>" \
.format("http://www.baidu.com","post","200","404")
@allure.description("简单描述")
def test_dynamic_desc():
# 断言成功之后,变更描述信息
assert 1 == 1
allure.dynamic.description(desc)
@allure.title("原始标题")
def test_dynamic_title():
assert 2 + 2 == 4
allure.dynamic.title("当断言成功时,用例标题会动态更新")
@allure.title("参数化用例标题:添加{param1}和{param2}")
@pytest.mark.parametrize('param1,param2,expected',[(2,2,4),(1,2,5)])
def test_with_param_title(param1,param2,expected):
assert param1 + param2 == expected
allure.dynamic.title("变更标题")
if __name__ == '__main__':
pytest.main(['-s','test_case_06.py','--alluredir','./result','--clean-alluredir'])
os.system('allure generate ./result -o ./report_allure/ --clean')
4.allure生成环境配置
#environment.properties
在result的目录下添加一个environment.properties文件
systemVersion=win10
pythonVersion=3.6.0
allureVersion=2.13.0
baseUrl=http://192.168.1.x:8080
projectName=test
author=zz
email=123123123@qq.com
freind=Mr.ma
idl=wangjie
#environment.xml
在allure的result目录下添加一个environment.xml
<environment>
<parameter>
<key>Browser</key>
<value>Chrome</value>
</parameter>
<parameter>
<key>Browser.Version</key>
<value>63.0</value>
</parameter>
<parameter>
<key>Stand</key>
<value>Production</value>
</parameter>
</environment>
import os
import pytest
# # 测试数据
# test_datas = [
# #"username":"zz1","password":"123456" 是用例的输入数据
# # "success!" 是预期结果
# ({"username":"zz","password":"111111"},"success!"),
# ({"username":"xz","password":"222222"},"failed!"),
# ({"username":"qs","password":"333333"},"success!")
# ]
# pytest用例函数
# @pytest.mark.parametrize("a,b",[('zz','123456'),('xz','123456'),('qs','123456')])
# def test_login(a,b):
# """测试登录用例"""
# print("登录")
# print(a)
# print(b)
# @pytest.mark.parametrize("test_input,expected",test_datas)
# def test_login(test_input,expected):
# """测试登录用例"""
# print("登录")
# # 获取输入数据
# print("获取输入数据")
# print(test_input["username"])
# print(test_input["password"])
# # 获取预期结果
# print("获取预期结果")
# print(expected)
# 模拟定义登录接口
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
# 返回
return {
"code":0,"msg":"success!"}
# # 测试数据
test_datas = [
#"username":"zz1","password":"123456" 是用例的输入数据
# "success!" 是预期结果
({
"username":"zz","password":"111111"},"success!"),
({
"username":"xz","password":"222222"},"failed!"),
({
"username":"qs","password":"333333"},"success!")
]
@pytest.mark.parametrize("test_input,expected",test_datas)
def test_login(test_input,expected):
"""测试登录用例"""
# 获取函数返回结果
result = login(test_input["username"],test_input["password"])
# 断言
assert result["msg"] == expected
if __name__ == '__main__':
# 生成报告数据源到result目录,存储json数据
pytest.main(['test_a_07.py','--alluredir','./result'])
# 基于json数据生成web报告工程
os.system('allure generate ./result -o ./report_allure/ --clean')
import os
import pytest
# # 测试数据
# test_datas = [
# #"username":"zz1","password":"123456" 是用例的输入数据
# # "success!" 是预期结果
# ({"username":"zz","password":"111111"},"success!"),
# ({"username":"xz","password":"222222"},"failed!"),
# ({"username":"qs","password":"333333"},"success!")
# ]
# pytest用例函数
# @pytest.mark.parametrize("a,b",[('zz','123456'),('xz','123456'),('qs','123456')])
# def test_login(a,b):
# """测试登录用例"""
# print("登录")
# print(a)
# print(b)
# @pytest.mark.parametrize("test_input,expected",test_datas)
# def test_login(test_input,expected):
# """测试登录用例"""
# print("登录")
# # 获取输入数据
# print("获取输入数据")
# print(test_input["username"])
# print(test_input["password"])
# # 获取预期结果
# print("获取预期结果")
# print(expected)
# 模拟定义登录接口
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
# 返回
return {
"code":0,"msg":"success!"}
# # 测试数据
test_datas = [
#"username":"zz1","password":"123456" 是用例的输入数据
# "success!" 是预期结果
({
"username":"zz","password":"111111"},"success!"),
({
"username":"xz","password":"222222"},"failed!"),
({
"username":"qs","password":"333333"},"success!")
]
@pytest.mark.parametrize("test_input,expected",test_datas)
def test_login(test_input,expected):
"""测试登录用例"""
# 获取函数返回结果
result = login(test_input["username"],test_input["password"])
# 断言
assert result["msg"] == expected
if __name__ == '__main__':
# 生成报告数据源到result目录,存储json数据
pytest.main(['test_a_08.py','--alluredir','./result','--clean-alluredir'])
# 基于json数据生成web报告工程
os.system('copy environment.properties result\\environment.properties')
os.system('allure generate ./result -o ./report_allure/ --clean')
5.Allure报告添加失败截图
conftest.py
import allure
import pytest
from selenium import webdriver
@pytest.fixture(scope = "session")
def browser():
# 定义全局变量
global driver
# 初始化浏览器对象并启动浏览器
driver = webdriver.Chrome()
# 返回浏览器对象给用例
yield driver
# 用例的后置处理:关闭浏览器
driver.quit()
print("test end!!")
""" 装饰器@pytest.hookimpl(hookwrapper=True)等价于@pytest.mark.hookwrapper的作用: a.可以获取测试用例的信息,比如用例函数的描述 b.可以获取测试用例的结果,yield,返回一个result对象 """
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport():
# 可以获取测试用例的执行结果,yield,返回一个result对象
out = yield
""" 从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象 report对象的属性 包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、 outcome(用例的执行结果:passed,failed) """
report = out.get_result()
# 仅仅获取用例call阶段的执行结果,不包含setup和teardown
if report.when == 'call':
# 获取用例call执行结果为结果为失败的情况
xfail = hasattr(report,"wasfail")
if(report.skipped and xfail) or (report.failed and not xfail):
# 添加allure报告截图
with allure.step("添加失败截图。。"):
# 使用allure自带的添加附件的方法:三个参数分别为:源文件、文件名 、 文件类型
allure.attach(driver.get_screenshot_as_png(),"失败截图",allure.attachment_type.PNG)
from time import sleep
def test_baidu_case01(browser):
driver = browser
driver.get("http://www.baidu.com")
sleep(2)
# 定位到百度搜索框,然后输入关键字
driver.find_element('id','kw').send_keys('狗狗币')
sleep(2)
# 定位到搜索按钮,点击搜索
driver.find_element('id','su').click()
sleep(2)
assert driver.title == "11狗狗币_百度搜索"
import os
import pytest
def run():
pytest.main(['-v', '--alluredir', './result', '--clean-alluredir'])
os.system('allure generate ./result -o ./report_allure/ --clean')
if __name__ == '__main__':
run()
边栏推荐
- Jenkins自动化部署项目
- 高性能数据访问中间件 OBProxy(三):问题排查和服务运维
- 查阅所连接过的WiFi所有信息(含密码)(访问历史所有WiFi连接)
- LeetCode二叉树系列——515.最每个树行中找最大值
- jsArray array copy method performance test 2207292307
- The path to uniting the programmer: "titles bucket" to the highest state of pragmatic
- Huawei's 7-year-experienced software testing director, gives some advice to all friends who want to change careers to learn software testing
- 还在说软件测试没有中年危机?9年测试工程师惨遭淘汰
- Eleven BUUCTF questions (06)
- 无代码开发平台应用可见权限设置入门教程
猜你喜欢

还在说软件测试没有中年危机?9年测试工程师惨遭淘汰

shell 编程规范与变量

时序数据库在船舶风险管理领域的应用

Skywalking入门

Flask框架——Flask-Mail邮件

Classic test interview questions set - logical reasoning questions

Conversion between pytorch and keras (the code takes LeNet-5 as an example)

jsArray array copy method performance test 2207300823

No-code development platform all application settings introductory tutorial

【Advanced Mathematics】【7】Double Integral
随机推荐
八年测试经验,为何惨遭领导痛批:你写的测试文档还不如刚来的应届生
ESP32 反复重启问题 Arduino屏蔽断电探测器
js人均寿命和GDP散点图统计样式
eclipse连接SQL server数据库「建议收藏」
CF780G Andryusha and Nervous Barriers
What is defect analysis?An article takes you to understand the necessary skills of test engineers
跳槽前,把自己弄成卷王
ARC115F Migration
CF603E Pastoral Oddities
CF338E Optimize!
LeetCode二叉树系列——515.最每个树行中找最大值
“12306” 的架构到底有多牛逼
ddl and dml in sql (the difference between sql and access)
BI-SQL丨WHILE
cpu/CS and IP
Learning notes - 7 weeks as data analyst "in the first week: data analysis of thinking"
shell script flow control statement
数据中台建设(五):打破企业数据孤岛和提取数据价值
Data Middle Office Construction (5): Breaking Enterprise Data Silos and Extracting Data Value
MIMO雷达波形设计