Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytest complains about fixtures, then runs fine on nearly identical code [closed]

Tags:

python

pytest

I'm trying to learn pytest, and I'm having a hard time understanding the behavior of parameterize().

import pytest

@pytest.mark.parameterize("foo", [1, 2, 3, 4, 5, 6, 7, ])
def test(foo):
    assert foo % 2 == 0

Running py.test on this returns an error: 'fixture 'foo' not found'.

Running this nearly identical example code works perfectly fine! What's the difference, why is my attempt at using parametrize failing?

import pytest

@pytest.mark.parametrize("blarg,argle", [("3+5", 8), ("2+4", 6), ("6*9", 42), ])
def test_eval(blarg, argle):
    assert eval(blarg) == argle
like image 725
user3205089 Avatar asked Mar 10 '14 22:03

user3205089


People also ask

How often is a fixture function in pytest executed?

Fixtures with scope session will only be executed once per session. Every time you run pytest , it's considered to be one session.

Are pytest fixtures cached?

Pytest only caches one instance of a fixture at a time, which means that when using a parametrized fixture, pytest may invoke a fixture more than once in the given scope.

How does fixture work in pytest?

Fixtures define the steps and data that constitute the arrange phase of a test (see Anatomy of a test). In pytest, they are functions you define that serve this purpose. They can also be used to define a test's act phase; this is a powerful technique for designing more complex tests.

How many times will a fixture of module scope run?

Module: If the Module scope is defined, the fixture will be created/invoked only once per module. Class: With Class scope, one fixture will be created per class object. Session: With the Session scope, the fixture will be created only once for entire test session.


1 Answers

Unfortunately it's just a typo in parametrize word,

import pytest

@pytest.mark.parametrize("foo", [1, 2, 3, 4, 5, 6, 7, ])
def test(foo):
    assert foo % 2 == 0

works great.

like image 92
Fedor Gogolev Avatar answered Sep 24 '22 12:09

Fedor Gogolev