“无法使用提供的凭据登录”(在测试中)

时间:2015-01-29 20:42:14

标签: django django-testing django-tests

我正在为用户注册和登录编写测试用例,我测试了whit postman(chrome)并且它可以工作,但是测试用例并没有。 我使用djangorestframework-jwt进行身份验证

测试:

class PublicUserTests(APITestCase):

    def test_create_account(self):
        url = "/api/user/create/"
        data = {'email': 'clark@gmail.com', 'nombre': 'Clark', 'password': 'Clark'}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)

    def test_login(self):
        url = "/api/auth/token/"
        response = self.client.post(url, {"email": "clark@gmail.com", "password": "Clark"}, format='json')
        print(response.status_text)
        print(response.content)
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)

结果:

Creating test database for alias 'default'...
.BAD REQUEST
b'{"non_field_errors":["Unable to login with provided credentials."]}'
F
======================================================================
FAIL: test_login (user.tests.PublicUserTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/rizotas/Proyects/django/src/rescue/user/tests.py", line 86, in test_login
    self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
AssertionError: 400 != 200 : ReturnDict([('non_field_errors', ['Unable to login with provided credentials.'])])

----------------------------------------------------------------------
Ran 2 tests in 0.116s

FAILED (failures=1)
Destroying test database for alias 'default'...

非常感谢你的帮助:)。

1 个答案:

答案 0 :(得分:1)

未连接TestCase中的测试方法。因此,当test_login有效时,他看不到来自test_create_account的用户。

您需要在test_login登录前创建用户。