-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
pytest_generate_tests doesn't play with pytest.mark.parametrize #896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Workaround pytest-dev/pytest#896 Now we can use pytest.mark.parametrize() without running session/module scoped fixtures for each test.
@philpep does this also happen when using indirect=True? |
I've run into the same issue. I've found that it doesn't have to use both
|
I'm running into this issue as well, is there a workaround for this? |
@RonnyPfannschmidt indeed that doesn't occur with indirect=True This test pass: import pytest
CALL_COUNT = 0
@pytest.fixture(scope="module")
def fixture(request):
global CALL_COUNT
CALL_COUNT += 1
def pytest_generate_tests(metafunc):
if "fixture" in metafunc.fixturenames:
metafunc.parametrize("fixture", ["foo"], indirect=True, scope="module")
@pytest.mark.parametrize("param", ["bar", "zaz"])
def test_1(fixture, param):
global CALL_COUNT
assert CALL_COUNT == 1 |
Unfortunately #1766 didn't fix the issues here :( |
import pytest
import os
HAS_CAR = bool(os.environ.get("HAS_CAR"))
def pytest_generate_tests(metafunc):
if 'car' in metafunc.fixturenames:
metafunc.parametrize(
'car', ['red', 'green'],
scope='module', indirect=HAS_CAR)
if HAS_CAR:
@pytest.fixture(scope='module')
def car(request):
return request.param
@pytest.yield_fixture(scope='module')
def prepared_car(car):
print("\n{} CAR PREPARED".format(car))
yield car
print("\n{} CAR TAKEN AWAY".format(car))
@pytest.mark.parametrize('wheel', [1, 2, 3], scope='function')
def test_car_wheels(prepared_car, wheel):
print("CHECKING {} CAR, WHEEL NUMBER {}".format(prepared_car, wheel))
def test_car_steering(prepared_car):
print("CHECKING {} CAR, STEERING WHEEL".format(prepared_car)) is a more extended example |
It looks like I am running into this issue too, which is causing my heavy setup/teardown to be run on each function instead of per session. |
Consider this test case:
The output is:
The
fixture
is called twice here, howerver it's a module scoped fixture so I expect only one call.The bug doesn't occur when writting two tests instead of using
pytest.mark.parametrize
or when using@pytest.fixture(scope="module", param=["foo"]
instead ofpytest_generate_tests
.Maybe related to #635
Thanks
The text was updated successfully, but these errors were encountered: