# -*- coding: utf-8 -*-
import argparse, getpass
import json, unittest, urllib, urllib2, pycurl, StringIO
#import sys
#reload(sys)
#sys.setdefaultencoding('utf-8')
uri = 'https://api.github.com'
user = 'changyy'
password = ''
class APIQueryTestBase(unittest.TestCase):
def setUp(self):
self.uri = uri
self.user = user
self.password = password
def query(self, api, get=None, post=None, json_output=True, debug=False):
target = self.uri + api
if get <>None:
target = target + '?' + urllib.urlencode(get)
if debug:
print "Origin:", self.uri + api
print "Full:", target
print "GET:", get
print "POST:", post
c = pycurl.Curl()
c.setopt( pycurl.URL , target )
c.setopt( pycurl.FOLLOWLOCATION , True )
c.setopt( pycurl.SSL_VERIFYPEER , False )
#c.setopt( pycurl.SSL_VERIFYHOST , 0 )
c.setopt( pycurl.SSLVERSION, pycurl.SSLVERSION_SSLv3)
#c.setopt( pycurl.COOKIEFILE , '/tmp/pycurl' )
#c.setopt( pycurl.COOKIEJAR , '/tmp/pycurl' )
if post:
c.setopt(pycurl.POST, True)
c.setopt(pycurl.POSTFIELDS, urllib.urlencode(post) )
b = StringIO.StringIO()
c.setopt(pycurl.WRITEFUNCTION, b.write)
c.perform()
r = b.getvalue()
b.close()
b = StringIO.StringIO()
c.setopt(pycurl.WRITEFUNCTION, b.write)
if json_output:
return json.loads(r)
else:
return r
def doLogin(self):
ret = self.query(api='/login.php')
self.assertTrue( 'status' in ret )
self.assertFalse( 'token' in ret )
ret = self.query(api='/login.php', get={'user':self.user, 'password':self.password})
self.assertTrue( 'status' in ret )
self.assertTrue( ret['status'] )
self.assertTrue( 'token' in ret )
self.token = ret['token']
def doLogout(self):
ret = self.query(api='/logout.php', get={'user':self.user, 'token':self.token})
self.assertTrue( 'status' in ret )
self.assertTrue( ret['status'] )
self.token = None
'''
class LoginAPITestCase(APIQueryTestBase):
def runTest(self):
self.doLogin()
class LogoutAPITestCase(APIQueryTestBase):
def runTest(self):
self.doLogin()
token = self.token
self.doLogout()
ret = self.query(api='/login.php', get={'user':self.user, 'token':token} )
self.assertTrue( 'status' in ret )
self.assertFalse( 'token' in ret )
'''
class GithubUserAPITestCase(APIQueryTestBase):
def check_format(self, ret):
self.assertTrue( 'login' in ret )
self.assertTrue( 'id' in ret )
self.assertTrue( 'url' in ret )
self.assertTrue( 'blog' in ret )
def runTest(self):
ret = self.query(api='/users/'+user)
self.check_format(ret)
class GithubUserRepoAPITestCase(APIQueryTestBase):
def check_format(self, ret):
self.assertTrue( type(ret) == list )
def runTest(self):
ret = self.query(api='/users/'+user+'/repos', get={'page':1})
self.check_format(ret)
ret = self.query(api='/users/'+user+'/repos', get=[('page',1)] )
self.check_format(ret)
if __name__ == '__main__':
"""
parser = argparse.ArgumentParser(description='api auto testing')
parser.add_argument(
'--host', dest='host',
default=''
help='host'
)
parser.add_argument(
'--user', dest='account',
default='frank',
help='account'
)
parser.add_argument(
'--pass', dest='password',
help='password'
)
results = parser.parse_args()
uri = results.host
user = results.account
password = results.password
if password == None:
password = getpass.getpass()
"""
unittest.main()
api 单元测试
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 和任何做产品的公司一样,软件公司对其软件产品的质量也是十分看重。虽然任何公司都会有测试部门,但软件产品的质量不能完...
- 一、API的单元测试 1.首先创建test类,上一篇博客将的是手动创建,这里讲自动创建 1)在需要创建单元测试的c...
- 使用SpringMVC来实现一组对User对象操作的RESTful API,配合注释详细说明在Spring MVC...
- 首先,回顾并详细说明一下在快速入门中使用的@Controller、@RestController、@Request...
- 摘要 现在大部分的iOS应用都是先从网络获取数据,在做相应的处理,数据的获取一般是在异步线程里做,所以做单元测试比...