使用python语言来进行实现,在这里我们使用第三方的库requests,需要单独的安装下,安装的命令是:
pip install -U requests
见安装的截图:
安装成功后,如果可以在正常的导入,说明安装OK,见截图:
安装好requests好后,我们就可以开始了,关于requests有不清楚的,可以看Python接口测试之requests(七),这里我们直接来进行实战,我们再来进行回顾下我们需要做的事情,我们需要做的就是登录成功后,获取token,后面的接口都需要这个参数,所以我们需要把获取的token当作参数来进行传递,在这里我们来思考一个问题,在python的函数中,为什么会有返回值?返回值可以干什么?python中,函数的返回值是为了给另外一个函数传递参数,传递参数可以理解为是通过自动将对象赋值给本地变量名来实现。OK,下面我们就来使用实现这个过程,我们首先使用代码来实现登录的接口并且获取返回的json字符串,见实现的代码:
#!/usr/bin/env python
#-*-coding:utf-8-*-
import requests
def getToken():
r=requests.post(
url='https://api.parkang.com/v20/login',
json={"username":"apitoapi",
"password":"8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92",
"role":2},
headers={'Content-Type':'application/json;charset=UTF-8',
'Parkingwang-Client-Source':'ParkingWangAPIClientWeb'},timeout=5)
return r.json()
print getToken()
见执行函数getToken后获取的json字符串:
C:\Python27\python.exe D:/git/Python/UnitCI/blog/weke.py
{u'status': 0, u'msg': u'', u'data':
{u'token': u'ma7uW5KiqKpBYPQ6kBI1514038085951', u'id': 600192, u'name': u'\u81ea\u52a8\u5316\u6d4b\u8bd5'}}
Process finished with exit code 0
我们可以看到,获取的内容是json字符串,经过反序列化后,数据类型是字典(dict),按字典的方式来获取token就很简单的,见获取token的代码:
#!/usr/bin/env python
#-*-coding:utf-8-*-
import requests
def getToken():
r=requests.post(
url='https://ecapi.parkingwang.com/v4/login',
json={"username":"autoapi","password":"8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92","role":2},
headers={'Content-Type':'application/json;charset=UTF-8',
'Parkingwang-Client-Source':'ParkingWangAPIClientWeb'},timeout=5)
return r.json()['data']['token']
见执行后,获取的token字符串,见截图:
下面来实现infoGet接口的测试,依据前面的案例我们知道,该接口的参数就是token,而且必须是和登录成功后返回的token是一致,否则就会提示502的错误,token无效,我们来实现infoGet接口测试,见实现代码:
#!/usr/bin/env python
#-*-coding:utf-8-*-
import requests
def getToken():
r=requests.post(
url='https://ecapi.parkingwang.com/v4/login',
json={"username":"autoapi","password":"8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92","role":2},
headers={'Content-Type':'application/json;charset=UTF-8','Parkingwang-Client-Source':'ParkingWangAPIClientWeb'},timeout=5)
return r.json()['data']['token']
def infoGet():
r=requests.post(
url='https://ecapi.parkingwang.com/v4/infoGet',
json={"token":getToken()},
headers={'Content-Type':'application/json;charset=UTF-8','Parkingwang-Client-Source':'ParkingWangAPIClientWeb'},timeout=5)
print r.json()
infoGet()
见执行infoGet函数后,返回的json字符串内容:
C:\Python27\python.exe D:/git/Python/UnitCI/blog/weke.py
{u'status': 0, u'msg': u'', u'data': {u'username': u'autoapi', u'online_agreement': 0, u'name': u'\u81ea\u52a8\u5316\u6d4b\u8bd5', u'valid_period': 0, u'id': 600192, u'phone': u'', u'pigeon_id': u'maLdrGaCEMAoS9kYW9t1514038531444', u'address': u'', u'supports': u'0', u'email': u'', u'park_code': 6666666666L}}
我们接着加一个接口,该接口为isSoonExpire,见测试该接口实现的代码:
#!/usr/bin/env python
#-*-coding:utf-8-*-
import requests
def getToken():
r=requests.post(
url='https://ecapi.parkingwang.com/v4/login',
json={"username":"autoapi","password":"8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92","role":2},
headers={'Content-Type':'application/json;charset=UTF-8',
'Parkingwang-Client-Source':'ParkingWangAPIClientWeb'},timeout=5)
return r.json()['data']['token']
def infoGet():
r=requests.post(
url='https://ecapi.parkingwang.com/v4/infoGet',
json={"token":getToken()},
headers={'Content-Type':'application/json;charset=UTF-8',
'Parkingwang-Client-Source':'ParkingWangAPIClientWeb'},timeout=5)
print r.json()
def isSoonExpire():
r=requests.post(
url='https://ecapi.parkingwang.com/v4/isSoonExpire',
json={"token":getToken()},
headers={'Content-Type':'application/json;charset=UTF-8',
'Parkingwang-Client-Source':'ParkingWangAPIClientWeb'},timeout=5)
print r.json()
下面我们对代码进行重构下,引入unittest,见重构后的代码:
#!/usr/bin/env python
#-*-coding:utf-8-*-
import requests
import unittest
class InterfaceTest(unittest.TestCase):
def setUp(self):
self.url='https://ecapi.parkingwang.com/v4/'
self.headers={'Content-Type':'application/json;charset=UTF-8',
'Parkingwang-Client-Source':'ParkingWangAPIClientWeb'}
self.timeout=5
def tearDown(self):
pass
def getToken(self):
r = requests.post(
url=self.url+'login',
json={"username": "autoapi",
"password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92",
"role": 2},
headers=self.headers, timeout=self.timeout)
return r.json()['data']['token']
def test_infoGet(self):
'''验证:测试infoGet接口是否正确'''
r = requests.post(
url=self.url+'infoGet',json={"token": self.getToken()},
headers=self.headers, timeout=self.timeout)
print r.json()
def test_isSoonExpire(self):
'''验证:测试isSoonExpire接口是否正确'''
r = requests.post(
url='https://ecapi.parkingwang.com/v4/isSoonExpire',
json={"token":self.getToken()},headers=self.headers, timeout=5)
print r.json()
if __name__ == '__main__':
unittest.main(verbosity=2)
如上的接口测试用例缺少断言,在这里特别强调,没有断言的自动化测试用例是无效的,是没有任何的价值的。至于具体断言什么,得格局具体的接口测试的业务来进行,我们对如上的接口添加断言,见增加断言后的接口测试代码:
#!/usr/bin/env python
#-*-coding:utf-8-*-
import requests
import unittest
import time as t
class InterfaceTest(unittest.TestCase):
def setUp(self):
self.url='https://ecapi.parkingwang.com/v4/'
self.headers={'Content-Type':'application/json;charset=UTF-8',
'Parkingwang-Client-Source':'ParkingWangAPIClientWeb'}
self.timeout=5
def tearDown(self):
t.sleep(1)
def getToken(self):
r = requests.post(
url=self.url+'login',
json={"username": "autoapi",
"password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92",
"role": 2},
headers=self.headers, timeout=self.timeout)
return r.json()['data']['token']
def test_infoGet(self):
'''验证:测试infoGet接口是否正确'''
r = requests.post(
url=self.url+'infoGet',json={"token": self.getToken()},
headers=self.headers, timeout=self.timeout)
self.assertEqual(r.json()['status'],0)
self.assertEqual(r.json()['data']['username'],'autoapi')
def test_isSoonExpire(self):
'''验证:测试isSoonExpire接口是否正确'''
r = requests.post(
url='https://ecapi.parkingwang.com/v4/isSoonExpire',
json={"token":self.getToken()},headers=self.headers, timeout=5)
self.assertEqual(r.json()['status'],0)
self.assertEqual(r.json()['data']['expire'],False)
if __name__ == '__main__':
unittest.main(verbosity=2)
执行上面的测试用例后,我们发现二个测试用例都调用了getToken()方法,而getToken()方法我们知道是登录的接口,登录成功后获取token,言外之意也就是说登录了二次,缺点很明显,如果在N个测试用例,参数都需要token,都会调用getToken(),那么也会出现登录N次,很实现这个方法不是我们想要的,我们要实现的是不管有多少个接口测试用例,登录只能是一次,如果登录多次,会出现token无效502的问题,或者引起其他的问题,在这里我们把登录成功后的token存储在文件中,然后从文件中读取,这样就可以登录一次,见实现的代码:
#!/usr/bin/env python
#-*-coding:utf-8-*-
import requests
import unittest
import time as t
import os
def getHeaders():
'''获取headers'''
return {'Content-Type':'application/json;charset=UTF-8',
'Parkingwang-Client-Source':'ParkingWangAPIClientWeb'}
def login():
'''把token写入到文件中'''
r = requests.post(
url=self.url + 'login',
json={"username": "autoapi",
"password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92",
"role": 2},
headers=getHeaders(), timeout=5)
with open(base_dir(), 'w') as f:
f.write(r.json()['data']['token'])
def base_dir():
'''获取当前文件的目录'''
return os.path.join(os.path.dirname(__file__), 'token.md')
def getToken():
'''读取存储在文件中的token'''
with open(base_dir(),'r') as f:
return f.read()
class InterfaceTest(unittest.TestCase):
def setUp(self):
self.url='https://ecapi.parkingwang.com/v4/'
def tearDown(self):
t.sleep(1)
def test_infoGet(self):
'''验证:测试infoGet接口是否正确'''
r = requests.post(url=self.url+'infoGet',
json={"token": getToken()},headers=getHeaders(), timeout=5)
self.assertEqual(r.json()['status'],0)
self.assertEqual(r.json()['data']['username'],'autoapi')
def test_isSoonExpire(self):
'''验证:测试isSoonExpire接口是否正确'''
r = requests.post(url=self.url+'isSoonExpire',
json={"token":getToken()},headers=getHeaders(), timeout=5)
self.assertEqual(r.json()['status'],0)
self.assertEqual(r.json()['data']['expire'],False)
if __name__ == '__main__':
unittest.main(verbosity=2)
代码经过重构后,不管有多少个测试用例,我们的登录只会进行一次。接下来我们会写关于session这部分,python是如何解决的。
python处理session
下面我们使用python来处理session的部分,下面我们来看一个例子,登录成功后,并没有返回token,只是返回了请求成功的code,而是存储session中,那么我们应该如何来获取session了,我们编写该登录的接口用例,看返回的json字符串是什么,见实现的代码以及输出的截图:
在截图中我们看到,输出的json字符串只有code是200的,再没有什么,但是后面的请求都需要带上session,我们看下一个接口请求的内容,见截图:
OK,那么就说明我们登录后必须得获取session,见获取session的代码:
通过cookies.get_dict()就可以获取session,见如上截图中获取的session内容,下面的就很简单,我们在下一个接口用例中带上session,见实现的码:
执行如上的代码后,我们发现代码错误,错误的原因是test_access_datas()函数没有带上登录成功后的session,所以导致错误,不带session,一般直接就跳转到了login的页面,让重新登录,那么如何解决了,我们只需要在test_access_datas()函数中,在接口请求中带上session就可以了,见修改后执行的代码: