pytest在一个功能中两次使用相同的灯具 [英] Pytest use same fixture twice in one function

查看:92
本文介绍了pytest在一个功能中两次使用相同的灯具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的Web服务器,我有一个登录固定装置,它创建一个用户并返回发送请求所需的标头。对于某个测试,我需要两个用户。如何在一个功能中两次使用相同的灯具?

For my web server, I have a login fixture that create a user and returns the headers needed to send requests. For a certain test, I need two users. How can I use the same fixture twice in one function?

from test.fixtures import login


class TestGroups(object):

    def test_get_own_only(self, login, login):
         pass


推荐答案

另一种方法是复制fixture函数。这既简单又正确,可以正确处理参数化的灯具,并使用两个灯具的所有参数组合调用测试函数。下面的示例代码引发了9个断言:

An alternative is just to copy the fixture function. This is both simple and correctly handles parameterized fixtures, calling the test function with all combinations of parameters for both fixtures. This example code below raises 9 assertions:

import pytest

@pytest.fixture(params=[0, 1, 2])
def first(request):
    return request.param

second = first

def test_double_fixture(first, second):
    assert False, '{} {}'.format(first, second)

这篇关于pytest在一个功能中两次使用相同的灯具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆