python测接口
import requests import unittest import HTMLTestRunner class interface(unittest.TestCase): def setUp(self): pass def test_case1(self): url = 'http://www.baidu.com' #百度 url1=url+'/a/b/c' #路径 req={ "transaction": { # 传参 "header": { "msg": { "msgCd": "a", "sndTm": "b", "rcvAppCd": "c", "sndAppCd": "d", "sndDt": " e", "seqNb": "f" }, "ver": "g" } }}} re=requests.post(url1,None,req) #发出请求,返回json re1=re.json() #json转换为字典 re2=str(re1) #字典转换成字符串 a0=True if '调用服务成功' in re2: a0=True else: a0=False self.assertEqual(a0,True) return re1 def test_case2(self): case1=interface() mm=case1.test_case1() m=mm['transaction']['body']['response']['salesList'] s='' for i in range(len(m)): s1 = '{"salesNo":"' + m[i]['salesNo'] + '","' + 'strNo":"' + m[i]['strNo'] + '","' + 'marNo":"' + m[i][ 'marNo'] + '"}' if i == len(m) - 1: s = s + s1 else: s = s + s1 + ',' print(s) url22='/a/b/c1' req2_1 = { "transaction": { "header": { "msg": { "msgCd": "a", "sndTm": "b", "rcvAppCd": "c", "sndAppCd": "d", "sndDt": "e", "seqNb": "f" }, "ver": "g" }, "body": { "request": { "areaMgrNo": "h", "optType": "i", "bindRelaList": s #拼装的字符串 } } } } print(req2_1) re_1 = requests.post(url22,None,req2_1) print(re_1) # re1_1 = re_1.json() # json转换为字典 # re2_1 = str(re1_1) # 字典转换成字符串 # print(re2_1) # a0 = True # if '调用服务成功' in re2_1: # a0 = True # else: # a0 = False # self.assertEqual(a0, True) if __name__=='__main__': unittest.main()