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
Fixtures with scope session will only be executed once per session. Every time you run pytest , it's considered to be one session.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With