Python脚本做接口测试如此简单之抛弃工具(二)

2019-10-25 17:29:47 浏览数 (1)

学习是为了更好的实践运用,之前做接口测试一直用的postman、fiddler,感觉用工具还是有些局限性,于是想着把学到的python灵活运用到接口测试中,于是就有了以下案例,思考着想用unittest做个简单的接口测试脚本,可以支持单传参和多传参,平时工作中可以随时使用,不但方便,还可以巩固到学习的知识,。

思考点:

1、脚本支持单传参和多传参,利用了for循环和ddt实现

2、接口headers里需传入登录后的token值,利用了unittest每次运行用例时先执行setup初始化好headers

代码语言:javascript复制
#unittest ddt实现的接口测试脚本



import requests,json,unittest,xlrd,os
from ddt import ddt,data,unpack
import requests

@ddt
class Test(unittest.TestCase):
    zp_url = 'http://test.com'  # 域名
    params = [{"self_introduce": "我是参数1!"}, {"self_introduce": "我是参数3!"}]  # 接口参数
    @classmethod
    def setUpClass(cls):
        cls.headers = {"Content-Type": "application/json", "Authorization": "token"}  # 请求头
        cls.zp_login_url = '/login'  # 登录接口地
        cls.zp_login_param = {"captcha": "1234", "client_type": 2, "login_type": 1, "mobile": 18221124103}  # 登录传参
        try:
            res = requests.post(url=cls.zp_url   cls.zp_login_url, headers=cls.headers, json=cls.zp_login_param)
            cls.headers['Authorization']=res.json()['data']['token']
        except Exception:
            print('参数错误')
        cls.url = '/update_user_info'  # 接口地址
        cls.method='POST'
    @classmethod
    def tearDownClass(cls):
        pass
    def sendGet(self,api_url,param):
        try:
            res=requests.get(url=api_url,headers=self.headers).json()
            print(res)
        except Exception:
            print('参数错误')
    def sendPost(self,api_url,param):
        try:
            res = requests.post(url=api_url, headers=self.headers, json=param).json()
            print(res)
        except Exception:
            print('参数错误')
    @data(*params)
    def test(self,params):
        if self.method=='GET' or self.method=='get' or self.method=='Get':
            self.sendGet(self.zp_url self.url,params)
        elif self.method== 'POST' or self.method=='post' or self.method=='Post':
            self.sendPost(self.zp_url self.url,params)
        else:
            print('请传正确请求方式GET或POST')
if __name__== '__main__':
    unittest.main()


"C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py
{'code': 0, 'data': {}, 'msg': '成功'}
..
----------------------------------------------------------------------
{'code': 0, 'data': {}, 'msg': '成功'}
Ran 2 tests in 0.462s

OK

Process finished with exit code 0
代码语言:javascript复制
#unittest实现的接口测试脚本


import requests,json,unittest,xlrd,os
from ddt import ddt,data,unpack
import requests,json
@ddt
class Test(unittest.TestCase):
    zp_url = 'http://test.com'  # 域名
    @classmethod
    def setUpClass(cls):
        cls.headers = {"Content-Type": "application/json", "Authorization": "token"}  # 请求头
        cls.zp_login_url = '/login'  # 登录接口地址
        cls.zp_login_param = {"captcha": "1234", "client_type": 2, "login_type": 1, "mobile": 18221124103}  # 熟仁直聘登录传参
        try:
            res = requests.post(url=cls.zp_url   cls.zp_login_url, headers=cls.headers, json=cls.zp_login_param)
            cls.headers['Authorization']=res.json()['data']['token']
        except Exception:
            print('参数错误')
        cls.url = '/update_user_info'  # 接口地址
        cls.params = [{"self_introduce": "我是参数1!"}, {"self_introduce": "我是参数3!"}]  # 接口参数
        cls.method='POST'
    @classmethod
    def tearDownClass(cls):
        pass
    def sendGet(self,api_url,param):
        try:
            res=requests.get(url=api_url,headers=self.headers).json()
            print(res)
        except Exception:
            print('参数错误')
    def sendPost(self,api_url,param):
        try:
            res = requests.post(url=api_url, headers=self.headers, json=param).json()
            print(res)
        except Exception:
            print('参数错误')
    def test(self):
        for param in self.params:
            if self.method=='GET' or self.method=='get' or self.method=='Get':
                self.sendGet(self.zp_url self.url,param)
            elif self.method== 'POST' or self.method=='post' or self.method=='Post':
                self.sendPost(self.zp_url self.url,param)
            else:
                print('请传正确请求方式GET或POST')
if __name__== '__main__':
    unittest.main()


"C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py
{'data': {}, 'code': 0, 'msg': '成功'}
.
----------------------------------------------------------------------
Ran 1 test in 0.515s

OK
{'data': {}, 'code': 0, 'msg': '成功'}

Process finished with exit code 0

0 人点赞