一、封装post/get方法
json.dumps() 将dict类型数据转化为str;
json.loads() 将str类型数据转化为dict;
json.dump() 将dict类型数据转化为str,并写入到json文件中;
json.load() 从json文件读取数据;
import requests
import json
class Leadscloud( ):
def send_post(self, url, data, headers):
response = requests.post(url=url, data=json.dumps(data), headers=headers).json() #是dict
#response = requests.post(url=url, data=data, headers=headers),是200
#json.dumps()将dict类型数据转化为str
print(json.dumps(response, indent=4, sort_keys=True, ensure_ascii=False))
return response
def send_get(self, url, data, headers):
response = requests.get(url=url, params=data, headers=headers).json() #是dict
# response = requests.get(url=url, params=data, headers=headers) #是200
#json.dumps()将dict类型数据转化为str
print(json.dumps(response, indent=4, sort_keys=True, ensure_ascii=False))
return response
def run_main(self, url, data, headers, method):
result = None
if method == 'post':
result = self.send_post(url, data, headers)
elif method == 'get':
result = self.send_get(url, data, headers)
else:
print("不是post方法,也不是get方法")
return result
if __name__ == '__main__':
url = " "
headers = {"Content-Type": "application/json; charset=UTF-8"}
parameter = {"visitorIdList": ["10110FK2019120214583914746", "10110FK2019111120131210678"]}
result1 = Leadscloud().run_main(url, data=parameter, headers=headers, method='post')
print(result1)
二、使用@ddt.file_data(‘文件路径’)实现数据驱动
@ddt :修饰测试类
@data():包含与要提供给测试的值一样多的参数。
@unpack:当传递的参数是复杂的数据结构时使用。如使用元组或者列表,添加unpack之后,它将自动将元组和列表解压缩为多个参数,并将字典解压缩为多个关键字参数。
@file_data('文件路径'):读取json(yaml)文件数据。
import unittest
import ddt
from leadscloud_common.common_method import Leadscloud
@ddt.ddt
class TestLogin(unittest.TestCase):
@ddt.file_data('E:\\interface\\leadscloud_test_data\\login_again.json')
def testLoginInterface(self, url, data, headers, method):
'''取json文件的 url, data, headers, method四个参数'''
data = Leadscloud().run_main(url, data, headers, method)
assert data.status_code == 200
# data.raise_for_status()
if __name__ == '__main__':
unittest.main(verbosity=2)
使用@ddt.data和@ddt.unpack
@ddt.data([{"123": ["123"]}], [{"hhhh": ["yyyy"]}])
@ddt.unpack
def test_visitorIdList_isNull(self, data):
try:
data = Leadscloud().run_main(url=self.url, data=data, headers=self.headers, method='post')
assert data['message'] == '参数不能为空'
except Exception as e:
raise e
@ddt.data([{"visitorIdList": 1}], [{"visitorIdList": 'abc'}], [{"visitorIdList": True}])
@ddt.unpack
def test_visitorIdList_isError(self, data):
try:
data = Leadscloud().run_main(url=self.url, data=data, headers=self.headers, method='post')
assert data['message'] == '参数格式有误'
except Exception as e:
raise e
requests.post(url='', files={'file': open('test.xls', 'rb')}, headers={'Content-Type': 'binary'})
requests.get(url='', params={'key1': 'value1', 'key2': 'value2'}, headers=headers)
requests.post(url='', data={'key1': 'value1', 'key2': 'value2'}, headers=headers)
**requests.post(url='', json={{'key1': 'value1', 'key2': 'value2'}}, headers=headers) (传入json格式文件)**
requests.post(url='', data=json.dumps({'key1': 'value1', 'key2': 'value2'}), headers=headers)(传入json格式文件)
三、json文件
test1及test2为两个测试用例;url,data,headers,method为四个参数
{
"test1": {"url": url1,
"data": {"userName": "[email protected]", "password": "212abddsfsdf"},
"headers": {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"},
"method": "post"
},
"test2": {"url": url2,
"data": {"userName": "liyali_10110", "password": "222"},
"headers": {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"},
"method": "post"
}
}