#https://www.cnblogs.com/miki-peng/p/12501341.html
#assert2.py
#注册方法判断
users=[{'username':'zhangsan123','password':'z123456'}]
def register(username,password1,password2):
if not all([username,password1,password2]):
return {'code':'0','msg':'所有参数不能为空'}
for user in users:
if username ==user['username']:
return {'code':'0','msg':'用户已经存在'}
else:
if password1 != password2:
return {'code':'0','msg':'两次密码输入不一样'}
else:
if 6<=len(username)<=18 and 6<=len(password2)<=18:
users.append({'username':username,'password':password2})
return {'code':'1','msg':'注册成功'}
else:
return {'code':'0','msg':'用户名和密码必须在6到18之间'}
#test_register.py
import unittest
#from Lib import HTMLTestRunnerNew# 如果要用HTML报告需要导入此模块
from assert2 import register # 导入被测试的代码
class TestRegister(unittest.TestCase):
def test_register_success(self):
'''接口注册成功用例'''
data=('wangkaiwei','w123456','w123456')
expected={'code':'1','msg':'注册成功'}
result=register(*data)# 把测试数据传到被测的代码register,接收实际结果
self.assertEqual(expected,result)
print('test_register_success测试通过')
def test_username_isnull(self):
'''接口注册失败username为空'''
data=('','w123456','w123456')
expected={'code':'0','msg':'所有参数不能为空'}
result=register(*data)# 把测试数据传到被测的代码register,接收实际结果
self.assertEqual(expected,result,'username不能为空')
print('test_username_isnull测试通过')
def test_username_existence(self):
'''用户名已经存在'''
data=('zhangsan123','z123456','z123456')
expected={'code':'0','msg':'用户已经存在'}
result=register(*data)
self.assertEqual(expected,result)
print('test_username_isnull测试通过')
if __name__ == '__main__':
unittest.main()
测试结果:
Ran 3 tests in 0.006s
OK
test_register_success测试通过
test_username_isnull测试通过
test_username_isnull测试通过