pytest.mark.parameterize 不“寻找”固定装置

问题描述 投票:0回答:4

我正在为一个小型库编写测试,在听到很多关于 py.test 的好消息后,我决定使用它。

但是,

pytest.mark.parameterize
给我带来了一些问题。起初,我想也许我只是不匹配一些括号,然后它就开始在其他地方寻找固定装置。所以我决定从给定的参数化示例开始:

@pytest.mark.parametrize("input,expected", [
    ("3+5", 8),
    ("2+4", 6),
    ("6*9", 42),
])
def test_eval(input, expected):
    assert eval(input) == expected

但这给出了同样的错误:

找不到固定装置“输入”

可用的装置:capfd、pytestconfig、recwarn、capsys、tmpdir、monkeypatch

使用“py.test --fixtures [testpath]”来获取帮助。

我去谷歌搜索,但找不到任何适用的答案。关于如何解决这个问题有什么想法吗?

编辑:我想知道哪些 Python/py.test 版本很有帮助。

Python 3.4.0 和 py.test 2.6.4

python pytest
4个回答
23
投票

我刚刚逐字尝试了你的示例,它在 pytest 2.6.4 中运行良好。也许您拼写错误

parametrize
?您在标题中拼写错误,这是一个常见错误,如本期所示。


10
投票

这不是相同的原因,但这是谷歌上的第一个结果“未找到pytest参数化固定装置”,这是我自然的谷歌与OP相同的错误:

未找到 E 装置“blah”

就我而言,这是由于一个愚蠢的拼写错误(我很久没有发现这个错误!),缺少装饰器中的@:

pytest.mark.parametrize("blah", [50, 100])
def test_something(blah):
    assert blah > 0

1
投票

就我而言,我将数据类更改为

@pytest.mark.parametrize

我忘记了

@pytest.mark.parametrize
是一个装饰器。 结果我没有把装饰器放在函数上面,装饰器和函数之间,还有其他函数。

@pytest.mark.parametrize("input,expected", [
    ("3+5", 8),
    ("2+4", 6),
    ("6*9", 42),
])

def test_otherfunctions():
    """ a function unrelated to the decorator. """
    pass

#the decorator should be here.
def test_eval(input, expected):
    assert eval(input) == expected

就我而言,修复只是重新排序。

def test_otherfunctions():
    """ a function unrelated to the decorator. """
    pass

@pytest.mark.parametrize("input,expected", [
    ("3+5", 8),
    ("2+4", 6),
    ("6*9", 42),
])
def test_eval(input, expected):
    assert eval(input) == expected

0
投票

记录我如何在不同的情况下得到这个错误,以防将来有人需要这个:

代码:

@pytest.mark.parametrize(
    "parameter1,parameter2",
    [('id_1', None), ('id_2', 'description')],
    ids=['W_P2', 'WO_P2'],
    indirect=True
)
def test_functionality(parameter1, parameter2):
    result = do_something(parameter1, parameter2)

    assert ...

错误:

E       fixture 'parameter1' not found
>       available fixtures: ...DELETED...
>       use 'pytest --fixtures [testpath]' for help on them.

原因: 我从我编写的其他测试中复制粘贴装饰器,并忘记删除

indirect=True
选项。

© www.soinside.com 2019 - 2024. All rights reserved.