一、接口自动化技术
requests+ pytest+allure
二、 requests使用
1.下载插件
pip install requests
2.使用
r = requests.get(url,data = {},headers = headers,timeout = 10,file = f)
r = requests.post(url,parms = {},headers = headers,timeout = 10,file = f)
r.text
r.json
r.status_code
r.content
r.cookies
r.headers
r.url
三、requests + pytest + allure实现自动化
准备csv数据
url,params,method
http://www.qubaobei.com/ios/cf/dish_list.php,"{""stage_id"": ""1"", ""limit"": ""20"", ""page"": ""1""}",get
http://www.wanandroid.com/article/list/0/json,{"cid":"60"},post读取csv文件
import csv
class ReadCsv:
def readCsv(self):
items = []
r = csv.reader(open('../data/a.csv'))
for i in r:
items.append(i)
return items-
结合读取的数据使用requests做http请求
import requestsfrom 接口自动化.read.readcsv import ReadCsv r = ReadCsv() content = r.readCsv() class UseRequests: def useRequests(self): codes = [] for i in contnet: if i[2]=='get': res = requests.get(i[0],data = i[1]) codes.append(res.status_code) eles: res = requests.post(i[0],parms = i[1]) codes.append(res.status_code) return codes
-
单元测试生成测试报告
import pytest, os
from 接口自动化.userequests.userequests import UseRequestsu = UseRequests() codes = u.useRequests() class TestPyRequest: def testPyRequest(self): for i in codes: assert i == 200 if __name__ == '__main__': pytest.main(['--alluredir', 'report/result', 'requesttestdemo.py']) split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean' os.system(split)