加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

资深程序员用Python实现数据驱动的接口自动化测试!

发布时间:2020-12-17 01:21:10 所属栏目:Python 来源:网络整理
导读:p style="margin-top:16px;color:rgb(34,34,34);font-family:'PingFang SC','Hiragino Sans GB','Microsoft YaHei','WenQuanYi Micro Hei','Helvetica Neue',Arial,sans-serif;background-color:rgb(255,255,255);" p style="margin-top:16px;color:rgb(34,2

<p style="margin-top:16px;color:rgb(34,34,34);font-family:'PingFang SC','Hiragino Sans GB','Microsoft YaHei','WenQuanYi Micro Hei','Helvetica Neue',Arial,sans-serif;background-color:rgb(255,255,255);">

资深程序员用Python实现数据驱动的接口自动化测试!

<p style="margin-top:16px;color:rgb(34,255);">2. 方案

<p style="margin-top:16px;color:rgb(34,255);">针对上面的API,在做接口测试时,需要的测试用例动辄会多达10+,这个时候采用数据驱动的方式将共性的内容写入配置文件或许会更合适。

<p style="margin-top:16px;color:rgb(34,255);">这里考虑把API、参数、以及预期结果预行在格式化的CSV里保存,利用csv组件从CSV里读取URL、参数以及预期结果,Requests组件发起请求,将响应结果与预期结果进行比对,最后把比对结果写到结果CSV。

<p style="margin-top:16px;color:rgb(34,255);">流程如下图

<p style="margin-top:16px;color:rgb(34,255);">

资深程序员用Python实现数据驱动的接口自动化测试!

<p style="margin-top:16px;color:rgb(34,255);">3. 实现

<p style="margin-top:16px;color:rgb(34,255);">(1) 在上代码之前,先安装好如下几个组件:

<ul class="list-paddingleft-2" style="margin-bottom:0px;padding:20px 30px;list-style:square outside;color:rgb(34,255);"><li style="margin-top:0px;margin-left:0px;padding:0px;list-style:inherit;"><p style="margin-bottom:0px;">csv 读写CSV文件

<li style="margin-top:0px;margin-left:0px;padding:0px;list-style:inherit;"><p style="margin-bottom:0px;">json

<li style="margin-top:0px;margin-left:0px;padding:0px;list-style:inherit;"><p style="margin-bottom:0px;">requests 发起请求,获取响应结果

<li style="margin-top:0px;margin-left:0px;padding:0px;list-style:inherit;"><p style="margin-bottom:0px;">unittest 测试用例调度

<p style="margin-top:16px;color:rgb(34,255);">(2) data.csv(本示例选取部分用例)

<p style="margin-top:16px;color:rgb(34,255);">

资深程序员用Python实现数据驱动的接口自动化测试!

<p style="margin-top:16px;color:rgb(34,255);">(3) reader_CSV函数代码示例

<p style="margin-top:16px;color:rgb(34,255);">

资深程序员用Python实现数据驱动的接口自动化测试!

<p style="margin-top:16px;color:rgb(34,255);">(4) request_URL函数示例(包含GET请求和POST请求2个方法)

<p style="margin-top:16px;color:rgb(34,255);">

资深程序员用Python实现数据驱动的接口自动化测试!

<p style="margin-top:16px;color:rgb(34,255);">(5) assert_Result函数示例

<p style="margin-top:16px;color:rgb(34,255);">

资深程序员用Python实现数据驱动的接口自动化测试!

<p style="margin-top:16px;color:rgb(34,255);">(6) write_CSV函数示例

<p style="margin-top:16px;color:rgb(34,255);">

资深程序员用Python实现数据驱动的接口自动化测试!

<p style="margin-top:16px;color:rgb(34,255);">(7) test_interface1函数示例

<p style="margin-top:16px;color:rgb(34,255);">

资深程序员用Python实现数据驱动的接口自动化测试!

<p style="margin-top:16px;color:rgb(34,255);">8result_1523956055.csv(本示例中的测试结果请忽略)

<p style="margin-top:16px;color:rgb(34,255);">

资深程序员用Python实现数据驱动的接口自动化测试!

<p style="margin-top:16px;color:rgb(34,255);">4. 总结

<p style="margin-top:16px;color:rgb(34,255);">留言给小编,小编会回复给大家PDF的文档!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读