Python 接口测试之ddt数据驱动番外篇
2023-02-26 10:15:25 时间
引言
前面我分享过一篇关于接口测试数据驱动方面的文章,文章是使用python第三方库ddt来进行数据驱动的。那如果没有这个库,我们不使用这个库,将如何进行数据分离呢?有没有思考过?
其实也是可以的,简单粗暴的方式利用excel存储测试数据,对常规操作的功能进行封装,也是可以搭建一套数据驱动的框架。
整体框架设计
画了一个草图,框架大致的思路是这样的:
项目结构
这里把项目结构贴出来,也方便大家脑补。
代码实现
这种设计方式的话,测试结果需要手动统计,然后写入邮件内容中发送出去。因为没有使用unittest框架,所以无法使用框架自带的html报告。但是为了可视化一下,所以手写了一个简单的报告模板,代码附上:
import time, os
# 数据部分
# case1 = {"name": "模块1", "total": "10", "passnum": "10", "failnum": "0", "radio": "80", "status": "PASS"}
case2 = {"name": "模块2", "total": "20", "passnum": "15", "failnum": "5", "radio": "75", "status": "Fail"}
VERSION_DICT = {"version": '快看小说 3.8.8', "radio": '99', "runstarttime": time.strftime('%Y-%m-%d %H:%M:%S'),
"runstoptime": time.strftime('%Y-%m-%d %H:%M:%S')}
class Template_mixin(object):
"""html报告"""
HTML_TMPL = r"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>自动化测试报告</title>
<link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<h2 style="font-family: Microsoft YaHei">自动化测试报告</h2>
<p class='attribute'><strong>测试结果 : </strong> %(value)s</p>
<style type="text/css" media="screen">
body { font-family: Microsoft YaHei,Tahoma,arial,helvetica,sans-serif;padding: 20px;}
</style>
</head>
<body>
<table id='result_table' class="table table-condensed table-bordered table-hover">
<colgroup>
<col align='left' />
<col align='right' />
<col align='right' />
<col align='right' />
</colgroup>
<tr id='header_row' class="text-center success" style="font-weight: bold;font-size: 14px;">
<th>客户端及版本</th>
<th>通过率</th>
<th>开始时间</th>
<th>结束时间</th>
</tr>
%(table_tr)s
<!-- 执行模块 -->
<p class='attribute'><strong>测试详情 : </strong> 执行结果</p>
<table id='result_table' class="table table-condensed table-bordered table-hover">
<colgroup>
<col align='left' />
<col align='right' />
<col align='right' />
<col align='right' />
<col align='right' />
</colgroup>
<tr id='header_row' class="text-center success" style="font-weight: bold;font-size: 14px;">
<th colspan="2">业务模块</th>
<th>用例总数</th>
<th>通过数</th>
<th>未执行</th>
</tr>
%(table_tr2)s
%(table_tr3)s
</table>
</body>
</html>"""
# 总数据
TABLE_TMPL_TOTAL = """
<tr class='failClass warning'>
<td>%(version)s</td>
<td>%(radio)s</td>
<td>%(runstarttime)s</td>
<td>%(runstoptime)s</td>
</tr>"""
# 详情表头
TABLE_TMPL_MODULE = """
<tr id='header_row' class="text-center success" style="font-weight: bold;font-size: 14px;">
<th>%(name)s</th>
<th>%(module)s</th>
<th>%(casetotal)s</th>
<th>%(passtotal)s</th>
<th>%(status)s</th>
</tr>"""
# case数据
TABLE_TMPL_CASE = """
<tr class='failClass warning'>
<td>%(name)s</td>
<td>%(module)s</td>
<td>%(casetotal)s</td>
<td>%(passtotal)s</td>
<td>%(status)s</td>
</tr>"""
if __name__ == '__main__':
table_tr0 = ''
table_tr1 = ""
table_tr2 = ""
numfail = 1
numsucc = 9
html = Template_mixin()
# 总表数据
table_td = html.TABLE_TMPL_TOTAL % dict(version=VERSION_DICT['version'], radio=VERSION_DICT['radio'],
runstarttime=VERSION_DICT['runstarttime'],
runstoptime=VERSION_DICT['runstoptime'])
table_tr0 += table_td
# 详情数据
# table_td_module = html.TABLE_TMPL_MODULE % dict(name="", module=case1["name"], casetotal=case1["total"],
# passtotal=case1["passnum"], status=case1["status"], )
# table_tr1 += table_td_module
# 表头总数
total_str = '共 %s,通过 %s,失败 %s' % (numfail + numsucc, numsucc, numfail)
# case数据
table_td_case = html.TABLE_TMPL_CASE % dict(name="", module=case2["name"], casetotal=case2["total"],
passtotal=case2["passnum"], status=case2["status"], )
table_tr2 += table_td_case
output = html.HTML_TMPL % dict(value=total_str, table_tr=table_tr0, table_tr2=table_tr1, table_tr3=table_tr2)
# 生成html报告
filename = '{date}_TestReport.html'.format(date=time.strftime('%Y%m%d%H%M%S'))
print(filename)
# 获取report的路径
curPath = os.path.abspath(os.path.dirname(__file__))
print(curPath)
dir = os.path.join(curPath, 'report')
print(dir)
filename = os.path.join(dir, filename)
with open(filename, 'wb') as f:
f.write(output.encode('utf8'))
结合整体框架,运行主程序后结果:
邮件内容:
再来看看自定义报告的内容:
这是一个简陋的报告,不像HTMLTestRunner那样,它是没有详情的数据的,当然,你感兴趣的话,也可以自行添加一些自定义的内容。
总结
以上就是不使用ddt和unittest框架,仅仅使用python基础代码来搭建的测试框架,并且实现了数据驱动,效果和使用ddt是一样的。希望这篇文章能帮助你学习接口测试,另外,对测试开发,自动化测试,全栈测试相关技术感兴趣的朋友,可以加入到群里学习和探索交流,进群方式,扫下方二维码。
相关文章
- 使用Python轻松获取Binance历史交易
- 良心推荐!Python爬虫高手必备的8大技巧!
- 用 Python 画如此漂亮的专业插图 ?简直 So easy!
- 用Python制作我的核酸检测日历
- 利用Python实现邮件发送
- 写一个爬取板块资金流的Python程序
- 学会这招真实用!复制粘贴,快速将Python程序打包成exe!
- 用 Python 的 Template 类生成文件报告
- TinyDB 一个纯Python编写的轻量级数据库
- Python中的十大图像处理工具
- 详细比对15款 Python 编辑器,请择优选用!
- 十个有趣的Python高级脚本,建议收藏!
- Python可视化 | Python可视化进阶必备 - plotly
- 用Python处理Excel的14个常用操作
- 17条好用的 Python 技巧分享!
- 最新开源:高效的 Python 通用对象池化库
- 微软出品的Python小白神器,真香!
- 用 Python 绘制几张有趣的可视化图表
- 整理了27个Python人工智能库,建议收藏!
- 细思恐极,插上 U 盘就开始执行 Python 代码