一、准备测试数据
在一个项目下(如study_unittest)新建一个test_cases文件夹,存放测试数据
在test_cases文件夹下新建一个test_login.py文件,使用unitest编写登录类测试一个简单的登录函数。代码如下:
def login(username=None, password=None):
"""登录"""
if (not username) or (not password):
# 用户名或者密码为空
return {"msg": "empty"}
if username == 'lay' and password == '123456':
# 正确的用户名和密码
return {"msg": "success"}
return {"msg": "error"}
#编写测试登录的类
import unittest
class TestLogin(unittest.TestCase):
#用户名、密码正确,登录成功
def test_login_01(self):
username="lay"
password="123456"
expected_result={"msg": "success"}
actual_result=login(username,password)
self.assertTrue(expected_result==actual_result)
#用户名为空,