简体   繁体   English

pytest 在参数化中使用时未找到夹具

[英]pytest fixture not found when used in parametrize

@pytest.fixture
def text():
    return "My fixture text for testing.\n"

@pytest.mark.parametrize(
    ("some_boolean_param", "text"),
    [
        (True, text), 
        (False, text),
    ],
)
def test_my_function(some_boolean_param, text, request):
    fixture_text = request.getfixturevalue(text)
    # do something with fixture_text and some_boolean param...

Results in:结果是:

E       fixture '<function text at 0x160acb280>' not found
>       available fixtures: fixture1, ..., text, another_fixture, ...

However if I put the fixture value inside a string, it just returns the string:但是,如果我将夹具值放在字符串中,它只会返回字符串:

@pytest.fixture
def text():
    return "My fixture text for testing.\n"

@pytest.mark.parametrize(
    ("some_boolean_param", "text"),
    [
        (True, "text"), 
        (False, "text"),
    ],
)
def test_my_function(some_boolean_param, text, request):
    fixture_text = request.getfixturevalue(text)

Results in:结果是:

>>> text
>>> "text"
>>> fixture_text
>>> "text"

How can I pass in the fixture to parametrize and get parametrize to find it?如何传入夹具进行参数化并进行参数化以找到它?

your code overrides the fixture name, please work with the following code:您的代码覆盖了夹具名称,请使用以下代码:

import pytest
@pytest.fixture
def text():
    return "My fixture text for testing.\n"

@pytest.mark.parametrize(
    ("some_boolean_param", "fixture_name"), # dont override fixture name
    [
        (True, "text"), 
        (False, "text"),
    ],
)
def test_my_function(some_boolean_param, fixture_name, request):
    fixture_text = request.getfixturevalue(fixture_name)
    print(f"text from fixture:{fixture_text}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM