def login_check(username,password):
'''
:param username: 登录用户名
:param password: 登录密码
:return:
'''
if 6<=len(password)<=18:
if username=='admin' and password=='123456':
return {'code':0,'msg':'登录成功'}
else:
return {'code':1,'msg':'账号密码不正确'}
else:
return {'code':1,'msg':'密码长度在6-18之间'}
设计测试用例验证上面登录是否正常
import unittest
from day2.day9 import login
from ddt import data,unpack,ddt
test_data=(('admin','123456','登录成功'),('adddd','123456','账号密码不正确'),
('admin','11234565','账号密码不正确'),('admin','12345','密码长度在6-18之间'),
('admin','12345678912345678912','密码长度在6-18之间'))
@ddt
class Login_test(unittest.TestCase):
@data(*test_data)
@unpack
#有了unpack后就会把输入数据分别输送给username,password,ex
def test_login(self,username,password,ex):
result= login.login_check(username,password)
excepted = result['msg']
try:
self.assertEqual(excepted,ex)
except AssertionError as e:
print("该用例未通过")
result = '不通过'
raise e
else:
print("该用例通过")
result = '通过'
# -*- coding:utf-8 -*-
import unittest
from HTMLTestRunner import HTMLTestRunner
from day2.day10 import dome_ddt
s=unittest.TestSuite()
l=unittest.TestLoader()
s.addTest(l.loadTestsFromTestCase(dome_ddt.Login_test))
with open(‘filename’,'wb' ) as f:
#filename为生成报告的文件名
test=HTMLTestRunner(
stream=f,
verbosity=2, #详细程度
title='测试报告',
description="第一份测试报告",
tester="NAME")
test.run(s)
import time
now = time.strftime("%m%d_%H_%M_%S",time.localtime(time.time()))
#%m、%d、%H、%M、%S分别表示月、日、时、分、秒
filename=r"想要保存的根目录"+now+r"report.html"
with open(filename,'wb' ) as f:
test=HTMLTestRunner(
stream=f,
verbosity=2, #详细程度
title='测试报告',
description="第一份测试报告",
tester="NAME")
test.run(s)