python测接口

Posted 来呀来呀

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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() 
  
 

  

以上是关于python测接口的主要内容,如果未能解决你的问题,请参考以下文章

pythonjava大作战,python测试dubbo接口

Python量化交易:数据获取接口

从父片段到选项卡片段的接口侦听器不起作用

Flask 编写http接口api及接口自动化测试

关于接口测试,我们要测什么,怎么测

面试题-websocket 接口如何测试?