装饰两个函数?

Decorate two functions?

是否可以通过使用装饰器或其他任何方式来缩短此类测试用例?

    def test_login_invalid_pwd(self):
        password = '12345'
        response = self._login(pwd=password)
        self.assertEqual(status_code, 200)
        self.assertEqual(response['resultText'],
                         'invalid password or login')
        self.assertEqual(response['resultCode'], 55)

    def test_web_login_invalid_login(self):
        login = 'my_1258@'
        response = self._login(login=login)
        self.assertEqual(status_code, 200)
        self.assertEqual(response['resultText'],
                         'invalid password or login')
        self.assertEqual(response['resultCode'], 55)

是的。试试这个:

def helper(self, response):
    self.assertEqual(status_code, 200)
    self.assertEqual(response['resultText'],
                     'invalid password or login')
    self.assertEqual(response['resultCode'], 55)

def test_login_invalid_pwd(self):
    password = '12345'
    response = self._login(pwd=password)
    self.helper(response)

def test_web_login_invalid_login(self):
    login = 'my_1258@'
    response = self._login(login=login)
    self.helper(response)

或者,根据您的测试所需的粒度,并假设您的 ._login() 方法使用 None 作为默认值:

def test_login_invalid(self):
    for login, pwd in (('my_1258@', None), (None, '12345')):
        response = self._login(login=login, pwd=pwd)
        self.assertEqual(status_code, 200)
        self.assertEqual(response['resultText'],
                         'invalid password or login')
        self.assertEqual(response['resultCode'], 55)