小编hiw*_*iwa的帖子

django 的 self.client.force_login 不起作用

我使用的是 django 3.2,测试包中的 self.client.force_login 无法登录。这是设置方法:

def setUp(self) -> None:
    self.user = User.objects.create_user(
        email='asdf@gmail.com',
        password='hiwa_asdf',
        name='smile as we go ahead'
    )
    self.client.force_login(self.user)
Run Code Online (Sandbox Code Playgroud)

这里是测试方法:

def test_update_uer_profile(self):
    """testing if update user profile works"""

    data = {
        'name': 'Angry as hell',
        'password': 'hi there john'
    }
    response = self.client.patch(me_url, data)
    print(response.content)
    self.assertEqual(response.status_code, status.HTTP_200_OK)
    self.user.refresh_from_db()
    self.assertEqual(self.user.name, data['name'])
    self.assertTrue(self.user.check_password(data['password']))
Run Code Online (Sandbox Code Playgroud)

结果如下:

Creating test database for alias 'default'...
System check identified no issues (0 silenced).
b'{"detail":"Authentication credentials were not provided."}'
F
======================================================================
FAIL: test_update_uer_profile (user.tests.test_user_api.PrivateApiTest)
testing if update …
Run Code Online (Sandbox Code Playgroud)

python django testcase

0
推荐指数
1
解决办法
2023
查看次数

标签 统计

django ×1

python ×1

testcase ×1