当前位置:网站首页>Allure Advanced - Dynamically Generate Report Content
Allure Advanced - Dynamically Generate Report Content
2022-07-30 14:30:00 【Si Xiaoyou】
目录
1.参数化结合allure.title()Generate different title report
1.参数化parametrize
import os
import pytest
# # 测试数据
# test_datas = [
# #"username":"zz1","password":"123456" Is a use case of input data
# # "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)
# To simulate the custom login interface
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
# 返回
return {
"code":0,"msg":"success!"}
# # 测试数据
test_datas = [
#"username":"zz1","password":"123456" Is a use case of input data
# "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__':
# Generate a report data sources toresult目录,存储json数据
pytest.main(['test_a_01.py','--alluredir','./result','--clean-alluredir'])
# 基于json数据生成webThe report projects
os.system('allure generate ./result -o ./report_allure/ --clean')
2.param+ids参数
import os
import pytest
# # 测试数据
# test_datas = [
# #"username":"zz1","password":"123456" Is a use case of input data
# # "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)
# To simulate the custom login interface
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
# 返回
return {
"code":0,"msg":"success!"}
# # 测试数据
test_datas = [
#"username":"zz1","password":"123456" Is a use case of input data
# "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__':
# Generate a report data sources toresult目录,存储json数据
pytest.main(['test_a_ids_02.py','--alluredir','./result2','--clean-alluredir'])
# 基于json数据生成webThe report projects
os.system('allure generate ./result2 -o ./report_allure2/ --clean')
3.allure.title描述用例
import os
import allure
import pytest
# # 测试数据
# test_datas = [
# #"username":"zz1","password":"123456" Is a use case of input data
# # "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)
# To simulate the custom login interface
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
# 返回
return {
"code":0,"msg":"success!"}
# # 测试数据
test_datas = [
#"username":"zz1","password":"123456" Is a use case of input data
# "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__':
# Generate a report data sources toresult目录,存储json数据
pytest.main(['test_a_title_03.py','--alluredir','./result3','--clean-alluredir'])
# 基于json数据生成webThe report projects
os.system('allure generate ./result3 -o ./report_allure3/ --clean')
4.优化用例title
import os
import allure
import pytest
# # 测试数据
# test_datas = [
# #"username":"zz1","password":"123456" Is a use case of input data
# # "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)
# To simulate the custom login interface
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
# 返回
return {
"code":0,"msg":"success!"}
# # 测试数据
test_datas = [
#"username":"zz1","password":"123456" Is a use case of input data
# "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__':
# Generate a report data sources toresult目录,存储json数据
pytest.main(['test_a_title_new_04.py','--alluredir','./result4','--clean-alluredir'])
# 基于json数据生成webThe report projects
os.system('allure generate ./result4 -o ./report_allure4/ --clean')
2.allureReport to empty the last record run
import os
import pytest
# # 测试数据
# test_datas = [
# #"username":"zz1","password":"123456" Is a use case of input data
# # "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)
# To simulate the custom login interface
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
# 返回
return {
"code":0,"msg":"success!"}
# # 测试数据
test_datas = [
#"username":"zz1","password":"123456" Is a use case of input data
# "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.allureThe situation of the report can be recorded cases each,方便跟踪用例的成功率,数据保留在json文件中
# 2.会带来一个问题,当你代码里的用例删除或者更换名称后,依然会记录之前的用例报告
# 3. --clean-alluredir Before each use case implementation to emptyallure的报告记录
pytest.main(['test_a_05.py','--alluredir','./result','--clean-alluredir'])
# 基于json数据生成webThe report projects
# 4.这里的cleanJust let the report can be regenerated,生成的结果,Case execution records will be kept before
os.system('allure generate ./result -o ./report_allure/ --clean')
3.allure动态生成用例标题
import os
import allure
# Interface use case description example
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():
# Assertion after successful,Change description information
assert 1 == 1
allure.dynamic.description(desc)
@allure.title("原始标题")
def test_dynamic_title():
assert 2 + 2 == 4
allure.dynamic.title("当断言成功时,Use case title will dynamic update")
@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
在resultAdd a directoryenvironment.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" Is a use case of input data
# # "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)
# To simulate the custom login interface
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
# 返回
return {
"code":0,"msg":"success!"}
# # 测试数据
test_datas = [
#"username":"zz1","password":"123456" Is a use case of input data
# "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__':
# Generate a report data sources toresult目录,存储json数据
pytest.main(['test_a_07.py','--alluredir','./result'])
# 基于json数据生成webThe report projects
os.system('allure generate ./result -o ./report_allure/ --clean')
import os
import pytest
# # 测试数据
# test_datas = [
# #"username":"zz1","password":"123456" Is a use case of input data
# # "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)
# To simulate the custom login interface
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
# 返回
return {
"code":0,"msg":"success!"}
# # 测试数据
test_datas = [
#"username":"zz1","password":"123456" Is a use case of input data
# "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__':
# Generate a report data sources toresult目录,存储json数据
pytest.main(['test_a_08.py','--alluredir','./result','--clean-alluredir'])
# 基于json数据生成webThe report projects
os.system('copy environment.properties result\\environment.properties')
os.system('allure generate ./result -o ./report_allure/ --clean')
5.AllureReport add failure screenshots
conftest.py
import allure
import pytest
from selenium import webdriver
@pytest.fixture(scope = "session")
def browser():
# 定义全局变量
global driver
# Initialization object browser and start the browser
driver = webdriver.Chrome()
# Returns the browser to use case
yield driver
# Use case rear processing:关闭浏览器
driver.quit()
print("test end!!")
""" 装饰器@pytest.hookimpl(hookwrapper=True)等价于@pytest.mark.hookwrapper的作用: a.可以获取测试用例的信息,比如用例函数的描述 b.You can get the result of the test case,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()
边栏推荐
- 无代码开发平台应用可见权限设置入门教程
- 六面蚂蚁金服,抗住面试官的狂轰乱炸,前来面试复盘
- ESP32 反复重启问题 Arduino屏蔽断电探测器
- Flask框架——Flask-SQLite数据库
- The website adds a live 2d kanban girl that can dress up and interact
- “12306” 的架构到底有多牛逼
- ccs软件的使用(靠谱挣钱的app软件)
- There is a risk of water ingress in the battery pack tray and there is a potential safety hazard. 52,928 Tang DMs are urgently recalled
- CF338E Optimize!
- Teach you how to write an eye-catching software testing resume, if you don't receive an interview invitation, I will lose
猜你喜欢

LeetCode二叉树系列——144.二叉树的最小深度

(一)Multisim安装与入门

Still saying software testing doesn't have a midlife crisis?9 years of test engineers were eliminated

LeetCode_98_验证二叉搜索树

ARC117E Zero-Sum Ranges 2

pytorch学习记录(六):循环神经网络 RNN & LSTM

权威推荐!腾讯安全DDoS边缘安全产品获国际研究机构Omdia认可

高性能数据访问中间件 OBProxy(三):问题排查和服务运维

近两年激光雷达运动物体分割论文阅读小结

mongodb打破原则引入SQL,它到底想要干啥?
随机推荐
JSON常用注解
华为7年经验的软件测试总监,给所有想转行学软件测试的朋友几点建议
ARC117E Zero-Sum Ranges 2
sql server安装失败怎么办(sql server安装不了怎么办)
双碳目标下:农田温室气体排放模拟
ARC115F Migration
ccs软件的使用(靠谱挣钱的app软件)
“12306” 的架构到底有多牛逼
UPC2022暑期个人训练赛第19场(B,P)
Flask框架——Sijax
八年测试经验,为何惨遭领导痛批:你写的测试文档还不如刚来的应届生
VLAN实验
以unity3d为例解读:游戏数据加密
pytorch与keras的相互转换(代码以LeNet-5为例)
EasyV数字孪生流域|宁波智慧水利整体智治综合应用
时序数据库在船舶风险管理领域的应用
OFDM Sixteen Lectures 3- OFDM Waveforms
CF1320E Treeland and Viruses
简单理解精确率(Precision),召回率(Recall),准确率(Accuracy),TP,TN,FP,FN
【ROS进阶篇】第十一讲 基于Gazebo和Rviz的机器人联合仿真(运动控制与传感器)