“errorMsg” : “”,
“data” : {
“config” : xxx
}
**Postman测试方法见**
### 测试思路
1.获取Postman原始脚本
2.使用requests库模拟发送HTTP请求\*\*
3.对原始脚本进行基础改造\*\*
4.使用python标准库里unittest写测试case\*\*
#### 原始脚本实现
##### 未优化
**该代码只是简单的一次调用,而且返回的结果太多,很多返回信息暂时没用,示例代码如下**
import requests
url = “http://cpright.xinhua-news.cn/api/match/image/getjson”
querystring = {“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, headers=headers, params=querystring)
print(response.text)
##### 优化 第一版
**调整代码结构,输出结果Json出来,获取需要验证的response.status\_code,以及获取结果校验需要用到的results['total']**
#!/usr/bin/env python
#coding: utf-8
‘’’
unittest merchant backgroud interface
@author: zhang_jin
@version: 1.0
@see:http://www.python-requests.org/en/master/
‘’’
import unittest
import json
import traceback
import requests
url = “http://cpright.xinhua-news.cn/api/match/image/getjson”
querystring = {
“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, headers=headers, params=querystring)
#对返回结果进行转义成json串
results = json.loads(response.text)
#获取http请求的status_code
print “Http code:”,response.status_code
#获取结果中的total的值
print results[‘total’]
#print(response.text)
##### 优化 第二版
**接口调用异常处理,增加try,except处理,对于返回response.status\_code,返回200进行结果比对,不是200数据异常信息。**
#!/usr/bin/env python
#coding: utf-8
‘’’
unittest merchant backgroud interface
@author: zhang_jin
@version: 1.0
@see:http://www.python-requests.org/en/master/
‘’’
import json
import traceback
import requests
url = “http://cpright.xinhua-news.cn/api/match/image/getjson”
querystring = {
“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, headers=headers, params=querystring)
#对http返回值进行判断,对于200做基本校验
if response.status_code == 200:
results = json.loads(response.text)
if results['total'] == 191:
print "Success"
else:
print "Fail"
print results['total']
else:
#对于http返回非200的code,输出相应的code
raise 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-8
‘’’
unittest 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_Logger
class MyTestSuite(unittest.TestCase):
“”“docstring for MyTestSuite”“”
#@classmethod
def sedUp(self):
print “start…”
#图片匹配统计
def test_image_match_001(self):
url = cf.URL1
querystring = {
"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, headers=headers, params=querystring)
if response.status_code == 200:
response.encoding = response.apparent_encoding
results = 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.URL2
querystring = {
"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, headers=headers, params=querystring)
if response.status_code == 200:
response.encoding = response.apparent_encoding
results = 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):
pass
if name == ‘main’:
#image_match_Logger = ALogger(‘image_match’, log_level=‘INFO’)
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
#image_match_Logger = ALogger(‘image_match’, log_level=‘INFO’)
[外链图片转存中…(img-EyBEbye2-1719275630848)]
[外链图片转存中…(img-4fM7mBVh-1719275630848)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!