justanr
justanr

Reputation: 750

pytest.mark.parameterize not "finding" fixtures

I'm writing tests for a small library and I decided to use py.test after hearing so many good things about it.

However, pytest.mark.parameterize is giving me some issues. At first, I thought maybe I just mismatched some parens and it went off looking for a fixture elsewhere. So I decided to start with the given example of parameterize:

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

But this gives the same error:

fixture 'input' not found

available fixtures: capfd, pytestconfig, recwarn, capsys, tmpdir, monkeypatch

use 'py.test --fixtures [testpath]' for help on them.

I went off googling, but I couldn't find any answers that applied. Any ideas on how to approach this?

Edit: I suppose knowing which Python/py.test versions is helpful.

Python 3.4.0 and py.test 2.6.4

Upvotes: 8

Views: 6912

Answers (4)

nnov
nnov

Reputation: 541

Documenting how I got this error in a different case, in the case someone needs this in the future:

Code:

@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 ...

Error:

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

Reason: I was copy pasting the decorator from other tests I wrote, and forgot to remove the indirect=True option.

Upvotes: 1

Constantin Hong
Constantin Hong

Reputation: 867

In my case, I changed a dataclass to @pytest.mark.parametrize.

I forgot @pytest.mark.parametrize is a decorator. As a result, I didn't put the decorator above the function, and between the decorator and the function, there were other functions.

@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

In my case, a fix is just a reorder.

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

Upvotes: 1

Bruno Oliveira
Bruno Oliveira

Reputation: 15255

I just tried your example verbatim and it worked fine in pytest 2.6.4. Perhaps you are misspelling parametrize? You misspelled it in the title and is a common mistake, as can be seen in this issue.

Upvotes: 26

Indivicivet
Indivicivet

Reputation: 216

It's not the same cause, but this is the first result on google for "pytest parametrize fixture not found", which was my natural google for the same error as OP:

E fixture 'blah' not found

In my case it was due to a silly typo (which I didn't spot for far too long!), missing the @ from the decorator:

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

Upvotes: 11

Related Questions