政务服务网站建设文档,一套完整的工程施工流程,seo是什么时候开始的,网站开发上海简介
本文通过从Postman获取基本的接口测试Code简单的接口测试入手#xff0c;一步步调整优化接口调用#xff0c;以及增加基本的结果判断#xff0c;讲解Python自带的Unittest框架调用#xff0c;期望各位可以通过本文对接口自动化测试有一个大致的了解。
引言
为什么要…简介
本文通过从Postman获取基本的接口测试Code简单的接口测试入手一步步调整优化接口调用以及增加基本的结果判断讲解Python自带的Unittest框架调用期望各位可以通过本文对接口自动化测试有一个大致的了解。
引言
为什么要做接口自动化测试?
在当前互联网产品迭代频繁的背景下回归测试的时间越来越少很难在每个迭代都对所有功能做完整回归。但接口自动化测试因其实现简单、维护成本低容易提高覆盖率等特点越来越受重视。
为什么要自己写框架呢?
使用Postman调试通过过直接可以获取接口测试的基本代码结合使用requets unittest很容易实现接口自动化测试的封装而且requests的api已经非常人性化非常简单但通过封装以后特别是针对公司内特定接口可以进一步提高脚本编写效率。
一个现有的简单接口例子
下面使用requests unittest测试一个查询接口
接口信息如下
请求信息: MethodPOST URLapi/match/image/getjson Request
{
category: image,
offset: 0,
limit: 30,
sourceId: 0,
metaTitle: ,
metaId: 0,
classify: unclassify,
startTime: ,
endTime: ,
createStart: ,
createEnd: ,
sourceType: ,
isTracking: true,
metaGroup: ,
companyId: 0,
lastDays: 1,
author:
}Response示例
{
timestamp : xxx,
errorMsg : ,
data : {
config : xxx
}Postman测试方法见
测试思路
1.获取Postman原始脚本
2.使用requests库模拟发送HTTP请求**
3.对原始脚本进行基础改造**
4.使用python标准库里unittest写测试case**
原始脚本实现
未优化
该代码只是简单的一次调用而且返回的结果太多很多返回信息暂时没用示例代码如下
import requestsurl http://cpright.xinhua-news.cn/api/match/image/getjsonquerystring {category:image,offset:0,limit:30,sourceId:0,metaTitle:,metaId:0,classify:unclassify,startTime:,endTime:,createStart:,createEnd:,sourceType:,isTracking:true,metaGroup:,companyId:0,lastDays:1,author:}headers {cache-control: no-cache,postman-token: e97a99b0-424b-b2a5-7602-22cd50223c15}response requests.request(POST, url, headersheaders, paramsquerystring)print(response.text)优化 第一版
调整代码结构输出结果Json出来获取需要验证的response.status_code以及获取结果校验需要用到的results[total]
#!/usr/bin/env python
#coding: utf-8unittest merchant backgroud interface
author: zhang_jin
version: 1.0
see:http://www.python-requests.org/en/master/
import unittest
import json
import traceback
import requestsurl http://cpright.xinhua-news.cn/api/match/image/getjsonquerystring {category: image,offset: 0,limit: 30,sourceId: 0,metaTitle: ,metaId: 0,classify: unclassify,startTime: ,endTime: ,createStart: ,createEnd: ,sourceType: ,isTracking: true,metaGroup: ,companyId: 0,lastDays: 1,author:
}headers {cache-control: no-cache,postman-token: e97a99b0-424b-b2a5-7602-22cd50223c15}#Post接口调用
response requests.request(POST, url, headersheaders, paramsquerystring)#对返回结果进行转义成json串
results json.loads(response.text)#获取http请求的status_code
print Http code:,response.status_code#获取结果中的total的值
print results[total]
#print(response.text)优化 第二版
接口调用异常处理增加tryexcept处理对于返回response.status_code返回200进行结果比对不是200数据异常信息。
#!/usr/bin/env python
#coding: utf-8unittest merchant backgroud interface
author: zhang_jin
version: 1.0
see:http://www.python-requests.org/en/master/
import json
import traceback
import requestsurl http://cpright.xinhua-news.cn/api/match/image/getjsonquerystring {category: image,offset: 0,limit: 30,sourceId: 0,metaTitle: ,metaId: 0,classify: unclassify,startTime: ,endTime: ,createStart: ,createEnd: ,sourceType: ,isTracking: true,metaGroup: ,companyId: 0,lastDays: 1,author:
}headers {cache-control: no-cache,postman-token: e97a99b0-424b-b2a5-7602-22cd50223c15}try:#Post接口调用response requests.request(POST, url, headersheaders, paramsquerystring)#对http返回值进行判断对于200做基本校验if response.status_code 200:results json.loads(response.text)if results[total] 191:print Successelse:print Failprint results[total]else:#对于http返回非200的code输出相应的coderaise Exception(http error info:%s %response.status_code)
except:traceback.print_exc()优化 第三版
1.该版本改动较大引入config文件单独封装结果校验模块引入unittest模块实现接口自动调用并增加log处理模块2.对不同Post请求结果进行封装不同接口分开调用3.测试用例的结果进行统计并最终输出
#!/usr/bin/env python
#coding: utf-8unittest interface
author: zhang_jin
version: 1.0
see:http://www.python-requests.org/en/master/
import unittest
import json
import traceback
import requests
import time
import result_statistics
import config as cf
from com_logger import match_Loggerclass MyTestSuite(unittest.TestCase):docstring for MyTestSuite#classmethoddef sedUp(self):print start...#图片匹配统计def test_image_match_001(self):url cf.URL1querystring {category: image,offset: 0,limit: 30,sourceId: 0,metaTitle: ,metaId: 0,classify: unclassify,startTime: ,endTime: ,createStart: ,createEnd: ,sourceType: ,isTracking: true,metaGroup: ,companyId: 0,lastDays: 1,author: }headers {cache-control: no-cache,postman-token: 545a2e40-b120-2096-960c-54875be347be}response requests.request(POST, url, headersheaders, paramsquerystring)if response.status_code 200:response.encoding response.apparent_encodingresults json.loads(response.text)#预期结果与实际结果校验调用result_statistics模块result_statistics.test_result(results,196)else:print http error info:%s %response.status_code#match_Logger.info(start image_query22222)#self.assertEqual(results[total], 888)try:self.assertEqual(results[total], 888)except:match_Logger.error(traceback.format_exc())#print results[total]#文字匹配数据统计def test_text_match_001(self):text_url cf.URL2querystring {category: text,offset: 0,limit: 30,sourceId: 0,metaTitle: ,metaId: 0,startTime: 2017-04-14,endTime: 2017-04-15,createStart: ,createEnd: ,sourceType: ,isTracking: true,metaGroup: ,companyId: 0,lastDays: 0,author: ,content: }headers {cache-control: no-cache,postman-token: ef3c29d8-1c88-062a-76d9-f2fbebf2536c}response requests.request(POST, text_url, headersheaders, paramsquerystring)if response.status_code 200:response.encoding response.apparent_encodingresults json.loads(response.text)#预期结果与实际结果校验调用result_statistics模块result_statistics.test_result(results,190)else:print http error info:%s %response.status_code#print(response.text)def tearDown(self): passif __name__ __main__:#image_match_Logger ALogger(image_match, log_levelINFO)#构造测试集合suiteunittest.TestSuite()suite.addTest(MyTestSuite(test_image_match_001))suite.addTest(MyTestSuite(test_text_match_001))#执行测试runner unittest.TextTestRunner()runner.run(suite)print success case:,result_statistics.num_successprint fail case:,result_statistics.num_fail#unittest.main()最终输出日志信息
Zj-Mac:unittest lazybone$ python image_test_3.py
测试结果通过.测试结果不通过
错误信息 期望返回值:190 实际返回值:4522.
----------------------------------------------------------------------
Ran 2 tests in 0.889sOK
success case: 1
fail case: 1后续改进建议
1.unittest输出报告也可以推荐使用HTMLTestRunner我目前是对结果统计进行了封装
2.接口的继续封装参数化模块化
3.unittest单元测试框架实现参数化调用第三方模块引用nose-parameterized
4.持续集成运行环境、定时任务、触发运行、邮件发送等一系列功能均可以在Jenkins上实现。
文章转载自: http://www.morning.nsppc.cn.gov.cn.nsppc.cn http://www.morning.nfcxq.cn.gov.cn.nfcxq.cn http://www.morning.dmlgq.cn.gov.cn.dmlgq.cn http://www.morning.yxnkr.cn.gov.cn.yxnkr.cn http://www.morning.rcmwl.cn.gov.cn.rcmwl.cn http://www.morning.yfmxn.cn.gov.cn.yfmxn.cn http://www.morning.cfcpb.cn.gov.cn.cfcpb.cn http://www.morning.snccl.cn.gov.cn.snccl.cn http://www.morning.tpqzs.cn.gov.cn.tpqzs.cn http://www.morning.mxxsq.cn.gov.cn.mxxsq.cn http://www.morning.rdfq.cn.gov.cn.rdfq.cn http://www.morning.ngcbd.cn.gov.cn.ngcbd.cn http://www.morning.pljxz.cn.gov.cn.pljxz.cn http://www.morning.mkpqr.cn.gov.cn.mkpqr.cn http://www.morning.ljhnn.cn.gov.cn.ljhnn.cn http://www.morning.krxzl.cn.gov.cn.krxzl.cn http://www.morning.dshxj.cn.gov.cn.dshxj.cn http://www.morning.ccpnz.cn.gov.cn.ccpnz.cn http://www.morning.mgskc.cn.gov.cn.mgskc.cn http://www.morning.tmsxn.cn.gov.cn.tmsxn.cn http://www.morning.tbjtp.cn.gov.cn.tbjtp.cn http://www.morning.knpbr.cn.gov.cn.knpbr.cn http://www.morning.grjh.cn.gov.cn.grjh.cn http://www.morning.zztmk.cn.gov.cn.zztmk.cn http://www.morning.zdsqb.cn.gov.cn.zdsqb.cn http://www.morning.tlbdy.cn.gov.cn.tlbdy.cn http://www.morning.knzmb.cn.gov.cn.knzmb.cn http://www.morning.kmrgl.cn.gov.cn.kmrgl.cn http://www.morning.nmkfy.cn.gov.cn.nmkfy.cn http://www.morning.ftmly.cn.gov.cn.ftmly.cn http://www.morning.rccpl.cn.gov.cn.rccpl.cn http://www.morning.jmtrq.cn.gov.cn.jmtrq.cn http://www.morning.kxnnh.cn.gov.cn.kxnnh.cn http://www.morning.npbnc.cn.gov.cn.npbnc.cn http://www.morning.wqkzf.cn.gov.cn.wqkzf.cn http://www.morning.lnsnyc.com.gov.cn.lnsnyc.com http://www.morning.mqwdh.cn.gov.cn.mqwdh.cn http://www.morning.ltspm.cn.gov.cn.ltspm.cn http://www.morning.qtrlh.cn.gov.cn.qtrlh.cn http://www.morning.mttqp.cn.gov.cn.mttqp.cn http://www.morning.nmbbt.cn.gov.cn.nmbbt.cn http://www.morning.swyr.cn.gov.cn.swyr.cn http://www.morning.spfq.cn.gov.cn.spfq.cn http://www.morning.kdlzz.cn.gov.cn.kdlzz.cn http://www.morning.djlxz.cn.gov.cn.djlxz.cn http://www.morning.rykmz.cn.gov.cn.rykmz.cn http://www.morning.lonlie.com.gov.cn.lonlie.com http://www.morning.lkrmp.cn.gov.cn.lkrmp.cn http://www.morning.chgmm.cn.gov.cn.chgmm.cn http://www.morning.fpzz1.cn.gov.cn.fpzz1.cn http://www.morning.gyfhk.cn.gov.cn.gyfhk.cn http://www.morning.gmmxh.cn.gov.cn.gmmxh.cn http://www.morning.xgchm.cn.gov.cn.xgchm.cn http://www.morning.rswfj.cn.gov.cn.rswfj.cn http://www.morning.rqgq.cn.gov.cn.rqgq.cn http://www.morning.yprjy.cn.gov.cn.yprjy.cn http://www.morning.nrqtk.cn.gov.cn.nrqtk.cn http://www.morning.lnbcg.cn.gov.cn.lnbcg.cn http://www.morning.jhzct.cn.gov.cn.jhzct.cn http://www.morning.fzlk.cn.gov.cn.fzlk.cn http://www.morning.sqhtg.cn.gov.cn.sqhtg.cn http://www.morning.sooong.com.gov.cn.sooong.com http://www.morning.huxinzuche.cn.gov.cn.huxinzuche.cn http://www.morning.rdkqt.cn.gov.cn.rdkqt.cn http://www.morning.nlqgb.cn.gov.cn.nlqgb.cn http://www.morning.tralution.cn.gov.cn.tralution.cn http://www.morning.zlrsy.cn.gov.cn.zlrsy.cn http://www.morning.hymmq.cn.gov.cn.hymmq.cn http://www.morning.kndst.cn.gov.cn.kndst.cn http://www.morning.ltdxq.cn.gov.cn.ltdxq.cn http://www.morning.yzxhk.cn.gov.cn.yzxhk.cn http://www.morning.rdmz.cn.gov.cn.rdmz.cn http://www.morning.kmcfw.cn.gov.cn.kmcfw.cn http://www.morning.lqjpb.cn.gov.cn.lqjpb.cn http://www.morning.jmbfx.cn.gov.cn.jmbfx.cn http://www.morning.rjhts.cn.gov.cn.rjhts.cn http://www.morning.rjbb.cn.gov.cn.rjbb.cn http://www.morning.swkzk.cn.gov.cn.swkzk.cn http://www.morning.clpkp.cn.gov.cn.clpkp.cn http://www.morning.knqck.cn.gov.cn.knqck.cn