将多个灯具传递到测试用例中

时间:2020-10-31 07:03:34

标签: python-3.x pytest

我正在将两个灯具setup_registered_consultant和setup_registered_customer传递到我的测试用例中,但是最后一个参数setup_registered_customer无法识别。我的测试用例在这里HTTP_AUTHORIZATION='Token ' + registered_response2.json()['token_key']失败,错误为test_userdetail_logged_in_customer_accesses_consultant_user_detail_view_success - KeyError: 'token_key'

def test_userdetail_logged_in_customer_accesses_consultant_user_detail_view_success(db, client, setup_registered_consultant, setup_registered_customer):
    registered_response = setup_registered_consultant
    registered_response2 = setup_registered_customer

    userdetail_response = client.get(path=reverse('user-detail', args=(str(registered_response.json()['id']),)),
                                      HTTP_AUTHORIZATION='Token ' + registered_response2.json()['token_key'])
    assert userdetail_response.status_code == status.HTTP_200_OK

如何通过一个测试用例传递多个灯具?

编辑:

@pytest.fixture
def setup_registered_customer(db, client):
    client.post(path=reverse('auto-signup-anonymous-user'),
                   data={"device_serial": "device1", "device_brand": "Apple",
                         "device_version": "1.0", "device_country": "US"})
    response = client.post(path=reverse('register'),
                            data={"device_serial": "device1", "username": "registereduser1",
                                    "email": "registereduser1@example.com", "password": "12345678"})
    return response

@pytest.fixture
def setup_registered_consultant(db, client):
    client.post(path=reverse('auto-signup-anonymous-user'),
                   data={"device_serial": "device1", "device_brand": "Apple",
                         "device_version": "1.0", "device_country": "US"})
    response = client.post(path=reverse('register'),
                            data={"device_serial": "device1", "username": "registereduser1",
                                    "email": "registereduser1@example.com", "password": "12345678"})
    get_user_model().objects.filter(username="registereduser1").update(is_consultant=True)
    ConsultantProfile.objects.create(user=get_user_model().objects.get(username="registereduser1"))
    return response

1 个答案:

答案 0 :(得分:1)

测试用例应在参数列表中接受任意数量的固定装置,并且顺序无关紧要。在这方面,您的代码看起来完全正确。

KeyError: 'token_key'提示变量registered_response2可能有问题,即JSON显然不包含“ token_key”键,因此最终这将意味着灯具本身存在问题,并且而不是传递给测试用例函数的方式。
正如问题注释中所讨论的,这里确实是这样:服务器由于后端大小发生冲突而返回了意外响应,这是由夹具如何调用服务器引起的。