#!/usr/bin/env python
#-*- coding:utf-8 -*-
# author:飞天P
import xlrd
import json
import unittest
import time as t
import requests
'''
接口测试: requests
GET请求:requests.get('https://github.com/timeline.json')
PUT请求:requests.put('http://httpbin.org/put')
POST请求:requests.post('http://httpbin.org/post')
DELETE请求:requests.delete('http://httpbin/ddelete')
'''
def readExcel(rowx,filePath="data.xlsx"):
'''
读取Excel中的数据并且返回
:param rowx: 在Excel中的行数
:param filePath: xlsx文件名称
:return:
'''
book = xlrd.open_workbook(filePath)
sheet = book.sheet_by_index(0)
return sheet.row_values(rowx)
# print("第一行的内容",readExcel(1))
# print("数据类型",format(type(readExcel(1))))
def getUrl(rowx):
'''
获取请求URL
:param rowx: 在Excel中的行数
:return:
'''
return readExcel(rowx)[1]
def getData(rowx):
'''
:param rowx: 在Excel中的行数
:return:
'''
return json.loads(readExcel(rowx)[2])
# print("读取",getUrl(1))
# print("读取",getData(1))
class ApiTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
t.sleep(1)
@classmethod
def tearDownClass(cls):
pass
def getHeaders(self):
return {
# 'Parkingwang-Client-Source':'ParkingWangAPIClientWeb',
'Content-Type':'application/json',##application/json;charset=UTF-8
'Cookie':'UM_distinctid=16909e89af33ff-01138263e034e6-8383268-1fa400-16909e89af499c; acw_tc=781bad2415506529985028541e7de1a6c0fd874a01b2547808f9ff52524588; _sm_au_c=iVV4Rn8FjZKpbRH50f; ASP.NET_SessionId=awvqip5xy02mwm5pyeevge0s; .ASPXAUTH=536F8BFEB7158AA7FD75746323EEE9EE3385ABE83034F942AF5F32390267E8116067CB9D00CBA73E60B614C1CC7CC8C1A889DDA23C46ABE666681693C279F15F277C9FA098840D5ED51A838D94A3690E4D326B76A3226C19F9C84BE65FF3A3B9F432D19DA249EB37ABD9BE9E7F8FCA5B3CFFA2DD8E332C9E98B7C45782052601'
}
def setproxy(self): #设置代理,没有的话可以不设
return {'http': '10.177.4.199:80'}
def test_post_001(self):
'''post请求,返回成功'''
r=requests.post(
url=getUrl(1),
json=getData(1),
proxies =self.setproxy(),
headers = self.getHeaders()
)
self.assertEqual(r.status_code,200)
print r.json()
def test_post_002(self):
'''requests post请求,参数是URL,data,json以及字典,proxies代理,headers'''
r=requests.post(
url = getUrl(2),
json = getData(2),
proxies = self.setproxy(),
headers = self.getHeaders()
)
print(r.text)
self.assertEqual(r.status_code,200)
re = int(r.json()['error']['Code'])
self.assertEqual(re,0)
if __name__ == "__main__":
unittest.main(verbosity=2)