数据驱动ddt
相比用for循环结合“一条一条加载用例”方式更为快捷的方法,是使用ddt
所有的测试用例所需的用例数据都作为类属性放在测试用例类中:
import unittest
from ddt import ddt, data
from exercise04_ddt.funcdemo_unittest import login_check
@ddt
class LoginTestCase(unittest.TestCase):
cases = [{"expected": {"code": 0, "msg": "登陆成功"}, "data": ["python", "123456"]},
{"expected": {"code": 1, "msg": "账号或密码不正确"}, "data": ["python", "12345"]},
{"expected": {"code": 1, "msg": "账号或密码不正确"}, "data": ["pyth", "123456"]},
{"expected": {"code": 1, "msg": "所有参数不能为空"}, "data": [None, "123456"]},
{"expected": {"code": 1, "msg": "所有参数不能为空"}, "data": ["python", None]},
]
@data(*cases)
def test_login(self, case):
expected = case["expected"]
data = case["data"]
res = login_check(*data)
self.assertEqual(expected, res)