首页 > 解决方案 > 误解了,来自文档Pytest授权的一个例子

问题描述

我决定看一下Pytest并立即误解,文档中的一个示例,但是没有授权,测试崩溃,代码为301,有人知道是什么原因吗?

def test_with_authenticated_client(client, django_user_model):
    username = "TestUser"
    password = "1234567"
    user = django_user_model.objects.create_user(username=username,
                                                 password=password)
    # Use this:
    client.force_login(user)
    response = client.get('/new')
    assert response.status_code == 200

def test_with_authenticated_client2(client):
    username = "user2"
    password = "bar"
    # Or this:
    client.login(username=username, password=password)
    response = client.get('/new')
    assert response.status_code == 200

与未经授权的客户,预期代码 301

def test_make_not_authorized_user(client):
    response = client.get('/new')
    assert response.status_code in (302, 301)

标签: pytestpytest-django

解决方案


推荐阅读