python unittest接口测试_Python+unittest 接口自动化测试

1、封装get、post

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

__author__ = 'hualai yu'

import requests

class RunMethod:

def post_main(self, url, data, header=None, params=None):

res = None

if header is not None:

res = requests.post(url=url, data=data, headers=header, params=params, verify=False)

else:

res = requests.post(url=url, data=data, verify=False)

return res

def get_main(self, url, data=None, header=None):

res = None

if header is not None:

res = requests.get(url=url, params=data, headers=header, verify=False)

else:

res = requests.get(url=url, params=data, verify=False)

return res

def run_main(self, method, url, data=None, header=None, params=None):

res = None

r = None

if method == 'Post':

res = self.post_main(url, data, header, params)

else:

res = self.get_main(url, data, header)

# return json.dumps(res, ensure_ascii=False)

print(res.url)

print(res.text)

# r = res.content

# par = json.loads(r)

return res.json()

2、接口用例测试

#!/usr/bin/env python

# -*- coding: utf-8 -*-

' a test module '

import urllib3

__author__ = 'yu'

import unittest

from methon.TestResult.conty.common import RunMethod

class Test(unittest.TestCase):

def setUp(self):

urllib3.disable_warnings()

self.run = RunMethod()

url = "https://x-chat-test.zmlearn.com/gateway/zhangmen-client-inClass/api/oauth/loginNew"

querystring = {"access_token": "undefined"}

data = "{\"msg\":\"QzCNH3+l2/P4w3jTqacK+VGcG2bZf1D1/Tk9qkaGhzxKkPz7h8btjdjWR542vq7zE70ouc+asLft1OpoijOcJwP+GP7" \

"/cnMTnRa1pe7bbwuc+66G1BDgnt4vHCPTk7wzxqmaWOaRFHi8SqheJo8zPVH2a13loqr7k9Xc83wEcRA=\"}\r\n "

header = {

'Content-Type': "application/json"

}

res = self.run.run_main("Post", url, data, header, querystring)

self.accessToken = res['data']['accessToken']

self.userId = res['data']['userId']

print(res)

def test_getInterviewRole_02(self):

url = "https://x-chat-test.zmlearn.com/gateway/zmc-personal-center/api/courseMsg/getUserMessages"

querystring = {"access_token": self.accessToken}

data = {

"userId": self.userId

}

header = {

"Content-Type": "application/x-www-form-urlencoded"

}

res = self.run.run_main("Post", url, data, header, querystring)

print(res)

# self.hall = res['data'][0]['id']

if __name__ == '__main__':

unittest.main()

标签:None,header,Python,res,unittest,接口,url,data,self

来源: https://www.cnblogs.com/v-hlyu/p/py.html

你可能感兴趣的:(python,unittest接口测试)