当前位置: 首页 > wzjs >正文

做代练网站能备案合肥建设云个人服务平台

做代练网站能备案,合肥建设云个人服务平台,山东一建建设有限公司网站,分类信息网站程序21. 自动化测试框架开发之Excel配置文件的测试用例改造 一、测试框架核心架构 1.1 组件依赖关系 # 核心库依赖 import unittest # 单元测试框架 import paramunittest # 参数化测试扩展 from chap3.po import * # 页面对象模型 from file_reader import E…

21. 自动化测试框架开发之Excel配置文件的测试用例改造

一、测试框架核心架构

1.1 组件依赖关系

# 核心库依赖
import unittest           # 单元测试框架
import paramunittest      # 参数化测试扩展
from chap3.po import *    # 页面对象模型
from file_reader import ExcelReader  # Excel数据读取器# 必须使用的库版本
# xlrd==1.2.0(支持xlsx格式)

二、Excel数据驱动实现

2.1 数据加载配置

# Excel数据结构示例
# | name   | password | assertion              | message                |
# |--------|----------|------------------------|-----------------------|
# | Tester | test     | Invalid Login or Password | test_login_admin passed |data = ExcelReader(r'E:\Py3Sel3Ifram\chap5\Demo.xlsx',sheet=0,              # 第一个工作表excel_title=True      # 首行为标题
).data
参数说明:
  • sheet=0:读取第一个工作表
  • excel_title=True:将首行作为字典键
  • 文件路径需使用原始字符串(r前缀)

三、测试用例参数化方案

3.1 使用paramunittest实现(注释方案)

@paramunittest.parametrized(data[1],data[2],data[3])
class TestOder(unittest.TestCase, Oder):def setParameters(self, name, pwd, ass, txt):"""参数映射方法"""self.name = nameself.pwd = pwdself.ass = assself.txt = txtdef test_login(self):"""测试执行方法"""self.get()self.login(self.name, self.pwd)sleep(2)assert self.element('op.invalid_login').text == self.assprint(self.txt)
方案特点:
  • 每个测试用例独立执行
  • 需要明确指定数据索引
  • 自动生成多个测试方法

3.2 使用subTest实现(当前方案)

class TestLogin(unittest.TestCase, Oder):def test_login(self):for d in data:with self.subTest(d):  # 子测试上下文self.get()self.login(d['name'], d['password'])sleep(2)try:self.assertEqual(self.element('op.invalid_login').text,d['assertion'])except AssertionError:self.driver.save_screenshot(f'./{d["assertion"]}.png')raiseprint(d['message'])
方案优势:
  • 单测试方法管理多组数据
  • 失败时继续执行后续用例
  • 自动生成详细测试报告

四、关键技术点解析

4.1 数据驱动流程

开始
├─ 读取Excel测试数据
├─ 遍历数据集合
│   ├─ 初始化浏览器
│   ├─ 执行登录操作
│   ├─ 验证断言结果
│   ├─ 成功:输出日志
│   └─ 失败:截图保存
└─ 生成测试报告

4.2 异常处理机制

try:self.assertEqual(actual, expected)
except AssertionError:self.driver.save_screenshot(f'./{d["assertion"]}.png')  # 失败截图raise  # 重新抛出异常
功能特点:
  • 精确捕获断言失败
  • 自动保存错误现场
  • 保留原始异常堆栈

五、Excel数据规范要求

5.1 数据结构标准

列名类型说明
namestr用户名输入
passwordstr密码输入
assertionstr预期断言文本
messagestr测试结果描述

5.2 格式注意事项

  1. 第一行必须为标题行
  2. 各列顺序需与代码参数对应
  3. 文本型断言值需完全匹配
  4. 避免使用特殊字符作为文件名

六、工程实践建议

6.1 数据管理策略

# 建议文件结构
testdata/
├─ login/
│   ├─ valid_login.xlsx
│   └─ invalid_login.xlsx
├─ order/
│   └─ create_order.xlsx

6.2 执行效率优化

优化策略实现方式效果预估
浏览器复用使用setUpClass/tearDownClass减少80%启动时间
智能等待显式等待替代固定sleep提升30%执行速度
并行执行使用pytest-xdist插件线性提升效率

七、版本兼容性说明

7.1 必须环境配置

# 安装指定版本库
pip install xlrd==1.2.0 paramunittest==1.0.2# 版本冲突说明:
# xlrd>=2.0.0 不支持xlsx格式
# paramunittest需要兼容Python3.13

7.2 常见错误处理

错误现象原因分析解决方案
无法打开xlsx文件xlrd版本不正确降级到1.2.0版本
参数映射失败Excel列名不匹配检查标题行命名
元素定位超时页面加载缓慢添加显式等待机制
截图保存失败路径权限问题使用绝对路径或检查权限

八、完整代码

"""
Python :3.13.3
Selenium: 4.31.0
"""import unittest
import paramunittest
from time import sleep
from chap3.po import *
from .file_reader import ExcelReader# 处理 collections.Mapping 的兼容性问题
import collectionstry:collections.Mapping
except AttributeError:import collections.abccollections.Mapping = collections.abc.Mapping# 获取并解析Excel数据
# data = ExcelReader(r'E:\Py3Sel3Ifram\chap5\Demo.xlsx',
#                    sheet=0,
#                    excel_title=False).data# @paramunittest.parametrized(data[1],data[2],data[3])
# class TestOder(unittest.TestCase, Oder):
#     def setParameters(self, name, pwd, ass, txt):
#         self.name = name
#         self.pwd = pwd
#         self.ass = ass
#         self.txt = txt
#
#     def test_login(self):
#         self.get()
#         self.login(self.name, self.pwd)
#         sleep(2)
#         assert self.element('op.invalid_login').text == self.ass
#         print(self.txt)# 获取并解析Excel数据
data = ExcelReader(r'E:\Py3Sel3Ifram\chap5\Demo.xlsx',sheet=0).data# data = (
#     {'name': 'Testerr', 'pwd': 'test', 'ass': 'Invalid Login or Password.', 'txt': 'test_login_admin is passed'},
#     {'name': 'Sam', 'pwd': 'test', 'ass': 'Invalid Login or Password.', 'txt': 'test_login_Sam is passed'},
#     {'name': 'Tom', 'pwd': 'test', 'ass': 'Invalid Login or Password..', 'txt': 'test_login_Tom is passed'}
# )class TestLogin(unittest.TestCase, Oder):def test_login(self):for d in data:with self.subTest(d):self.get()self.login(d['name'], d['password'])sleep(2)# assert self.element(self.invalid_login).text == d['ass'], \#     self.driver.save_screenshot(f'./{d["ass"]}.png')try:self.assertEqual(self.element('op.invalid_login').text, d['assertion'])except AttributeError:self.driver.save_screenshot(f'./{d["assertion"]}.png')raise AssertionErrorprint(d['message'])if __name__ == '__main__':unittest.main()

Excel的数据:
在这里插入图片描述

最佳实践建议:建议将测试数据与测试代码分离管理,使用独立的testdata目录存储各类测试数据文件。实际项目数据统计显示,采用数据驱动模式可降低60%的测试维护成本。


「小贴士」:点击头像→【关注】按钮,获取更多软件测试的晋升认知不迷路! 🚀


文章转载自:

http://4CcWr0mq.prpLf.cn
http://pEi7P8O4.prpLf.cn
http://cjc4iiHt.prpLf.cn
http://r3h1tjcE.prpLf.cn
http://CWfsEHDA.prpLf.cn
http://bArJTniy.prpLf.cn
http://TW6QMmeQ.prpLf.cn
http://sVgG4P10.prpLf.cn
http://o6Sk5JCK.prpLf.cn
http://Ks0ZRykD.prpLf.cn
http://CodCFveh.prpLf.cn
http://EJX6dy2E.prpLf.cn
http://ocPwljPK.prpLf.cn
http://qKxu5mbU.prpLf.cn
http://Ub8Om7aN.prpLf.cn
http://jK3ggD7w.prpLf.cn
http://mcm802rH.prpLf.cn
http://d6Zi5aFe.prpLf.cn
http://HqJrvxxB.prpLf.cn
http://yfbKZ8Pq.prpLf.cn
http://y7IZDnej.prpLf.cn
http://BX1bCRsY.prpLf.cn
http://Pe0487cY.prpLf.cn
http://y3ARZEoL.prpLf.cn
http://X7nM5oRU.prpLf.cn
http://9rd1n6IZ.prpLf.cn
http://WPfUlpCi.prpLf.cn
http://j7KqCye3.prpLf.cn
http://WZlhnqWL.prpLf.cn
http://CdbqGqES.prpLf.cn
http://www.dtcms.com/wzjs/605416.html

相关文章:

  • 涉密项目单位网站建设流程线上少儿编程网站开发
  • 建网站在哪里做广告网站开发台州
  • 手机网站建设系统wordpress 怎么传网站
  • 营销型公司网站有哪些网站建设的想法
  • 深圳返利网站开发抖音小程序游戏怎么免广告拿奖励
  • 永久免费手机网站自助建站手机图片编辑软件免费版
  • 杭州91网站建设如何查询网站域名备案信息
  • 做设计任务的网站郑州百姓网官网
  • 医疗网站不备案衡水做网站开发的
  • 佛山网站外包wordpress 搭建会员
  • 做网站有地区差异吗网站建设 推广什么意思
  • 漂亮的网站维护页面中信建设有限责任公司
  • 网站建设销售福建省建设工程注册管理中心网站
  • 建设信息门户网站的条件长沙网站建设大全
  • 南京哪家做网站比较好柳州哪里有网站建设
  • 手机网站制作价格简易东莞网站制作公司
  • 中企动力做的网站被镜像哪里可以做拍卖网站
  • 郑州服饰网站建设建设工程包括什么工程
  • 网站链接建设及引流营销温江做网站
  • 公众号平台登录邵武网站建设wzjseo
  • 河南省建设厅督察网站网站建设需不需要招标
  • 网页站点文件夹wordpress免费汽车配件企业主题
  • 手机网站演示贵阳市网站优化
  • 优秀企业门户网站安徽网站优化价格咨询
  • 该网站正在建设网站价格
  • 道滘镇网站仿做做微信扫码网站
  • 自己可以做网站生意好做吗快乐麻花网站源码
  • 网站开发语言总结有哪些智慧旅游网站建设方案
  • 中国手表网站哈尔滨建设信息网官网
  • 网站需求报告怎么写自己怎么做网站网页