Python pytest从功能范围夹具访问参数

Python pytest从功能范围夹具访问参数,python,pytest,fixtures,Python,Pytest,Fixtures,假设我有以下代码: @pytest.mark.parametrize("argument", [1]) def test_func(self, function_context, argument) 我有以下功能范围: @pytest.fixture(scope='function') def function_context(session_context): # .... do something .... 是否可以从function\u上下文中访问当前函数参

假设我有以下代码:

@pytest.mark.parametrize("argument", [1])
def test_func(self, function_context, argument)
我有以下功能范围:

@pytest.fixture(scope='function')
def function_context(session_context):
    # .... do something ....
是否可以从
function\u上下文中访问当前函数参数


在我的例子中-我想从
function\u context
中获取在
parametrize
中传递的值
1
pytest
中的fixture在实际测试运行之前被实例化,因此在fixture定义阶段不可能访问test function参数。然而,我可以想出两种方法来绕过这个问题:

1.蒙基修补 您可以根据使用该装置的函数的参数来更改装置,即临时更改其某些属性。例如:

@pytest.fixture(scope='function')
def function_context(session_context):
    # .... do something ....

@pytest.mark.parametrize("argument", [1])
def test_func(self, function_context, argument, monkeypatch):
    monkeypatch.setattr(function_context, "number", argument) # assuming you want to change the attribute "number" of the function context
    # .... do something ....
@pytest.fixture(scope='function', params=[0, 1])
def function_context(session_context, request):
    param = requests.param # now you can use param in the fixture
    # .... do something ...

def test_func(self, function_context):
    # .... do something ...
尽管您的fixture仅在函数范围内有效,但monkeypatching也仅在测试的一次运行中有效

2.参数化夹具而不是测试功能 或者,您也可以选择自动切换,而不是使用
test\u func
。例如:

@pytest.fixture(scope='function')
def function_context(session_context):
    # .... do something ....

@pytest.mark.parametrize("argument", [1])
def test_func(self, function_context, argument, monkeypatch):
    monkeypatch.setattr(function_context, "number", argument) # assuming you want to change the attribute "number" of the function context
    # .... do something ....
@pytest.fixture(scope='function', params=[0, 1])
def function_context(session_context, request):
    param = requests.param # now you can use param in the fixture
    # .... do something ...

def test_func(self, function_context):
    # .... do something ...

pytest
中的fixture是在实际测试运行之前实例化的,因此在fixture定义阶段不可能访问test function参数。然而,我可以想出两种方法来绕过这个问题:

1.蒙基修补 您可以根据使用该装置的函数的参数来更改装置,即临时更改其某些属性。例如:

@pytest.fixture(scope='function')
def function_context(session_context):
    # .... do something ....

@pytest.mark.parametrize("argument", [1])
def test_func(self, function_context, argument, monkeypatch):
    monkeypatch.setattr(function_context, "number", argument) # assuming you want to change the attribute "number" of the function context
    # .... do something ....
@pytest.fixture(scope='function', params=[0, 1])
def function_context(session_context, request):
    param = requests.param # now you can use param in the fixture
    # .... do something ...

def test_func(self, function_context):
    # .... do something ...
尽管您的fixture仅在函数范围内有效,但monkeypatching也仅在测试的一次运行中有效

2.参数化夹具而不是测试功能 或者,您也可以选择自动切换,而不是使用
test\u func
。例如:

@pytest.fixture(scope='function')
def function_context(session_context):
    # .... do something ....

@pytest.mark.parametrize("argument", [1])
def test_func(self, function_context, argument, monkeypatch):
    monkeypatch.setattr(function_context, "number", argument) # assuming you want to change the attribute "number" of the function context
    # .... do something ....
@pytest.fixture(scope='function', params=[0, 1])
def function_context(session_context, request):
    param = requests.param # now you can use param in the fixture
    # .... do something ...

def test_func(self, function_context):
    # .... do something ...