import unittest
from lib.tools import my_redis,my_request,my_mysql
from conf.settings import HOST_INFO
class Choujiang(unittest.TestCase):
def setUp(self):
self.choujiang_url = HOST_INFO + '/api/product/choice'
self.login_url = HOST_INFO + '/api/user/login'
self.reg_url = HOST_INFO + '/api/user/user_reg'
def tearDown(self): # tearDown是每条用例执行完运行的,正常抽奖执行完删除用户再注册,登录,抽奖
sql = 'delete from app_myuser where username = "sunyy124";' # 删除注册过的用户名,运行N次都OK
my_mysql.other_sql(sql)
def reg(self):
data = {'username': 'sunyy124', 'pwd': '123456aA', 'cpwd': '123456aA'}
res = my_request('post', self.reg_url, data)
print(res)
self.assertIsInstance(res, dict) # 校验类型,是否是字典,只有是字典才能进行继续判断,.json()返回字典
self.assertEqual(1000, res.get('error_code')) # 注册成功校验error_code是否是1000
self.assertEqual('注册成功!', res.get('msg')) # 注册成功校验msg是否是注册成功!
# sql = 'delete from app_myuser where username = "sunyy124";' # 删除注册过的用户名,运行N次都OK
# my_mysql.other_sql(sql)
def login(self):
data = {'username': 'sunyy124', 'passwd': '123456aA'}
res = my_request('post',self.login_url,data)
self.assertIsInstance(res, dict) # 校验类型,是否是字典,只有是字典才能进行继续判断,.json()返回字典
self.assertEqual(0, res.get('error_code')) # 登录成功校验error_code是否是0
self.sign = res.get('login_info').get('sign') # 登录成功获取到sign
self.userId = res.get('login_info').get('userId') # 登录成功获取到userId
def testChoujiang(self):
'''正常抽奖'''
self.reg() # 保证登录失效,要再调一下注册接口
self.login()
my_redis.set('choujiang:sunyy124',0) # 防止抽奖次数用尽,每次抽奖前都set为0,运行用例就不会报错
data = {'userid': self.userId,'sign': self.sign}
res = my_request('get',self.choujiang_url,data)
self.assertIsInstance(res,dict)
self.assertEqual(0,res.get('error_code')) # error_code是0表示抽奖成功
def testChoujiangErr(self):
'''抽奖次数用尽'''
self.reg() # 保证登录失效,要再调一下注册接口
self.login()
my_redis.set('choujiang:sunyy124',3) # 测试抽奖次数用尽,每次抽奖前都set为3,运行用例就不会报错
data = {'userid': self.userId,'sign': self.sign}
res = my_request('get',self.choujiang_url,data)
self.assertIsInstance(res, dict)
self.assertEqual(1099, res.get('error_code')) # error_code是1099表示抽奖次数已用尽
if __name__ == '__main__':
unittest.main()