我正在运行这样的登录测试:
def test_login_user(self):
client = APIClient()
url = reverse('rest_login')
data = {
'username': 'test',
'password': 'Welcome2'
}
response = self.client.post(url, data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
client.logout()
如果我正常登录应用程序,我会看到一个 json 返回,如下所示:
{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoyLCJ1c2VybmFtZSI6ImV2YW4iLCJleHAiOjE1MTQ2NzYzNTYsImVtYWlsIjoiZXZhbkAyOGJlYXR0eS5jb20iLCJvcmlnX2lhdCI6MTUxNDY3Mjc1Nn0.8CfhfgtMLkNjEaWBfNXbUWXQMZG4_LIru_y4pdLlmeI",
"user": {
"pk": 2,
"username": "test",
"email": "test@test.com",
"first_name": "",
"last_name": ""
}
}
我希望能够捕获那个
token
future 使用的值(value)但是response
好像没有data
值(value)抢。
最佳答案
我要找的是 response.content
根据官方文档
https://docs.djangoproject.com/en/2.0/topics/testing/tools/#testing-responses
关于django 测试 - 如何获取响应数据以备将来使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48046045/