Skip to content
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

Fixture are not meant to be called directly #60

Closed
Overdrivr opened this issue Jan 9, 2019 · 5 comments
Closed

Fixture are not meant to be called directly #60

Overdrivr opened this issue Jan 9, 2019 · 5 comments

Comments

@Overdrivr
Copy link

With pytest 4.1.0, it is no longer possible to pass a fixture to a parameterized test:

import pytest
from parameterized import parameterized

@pytest.fixture
def gen_test():
    return [('foo',), ('bar',)]

@parameterized(gen_test)
def test_(arg):
    print(arg)
Fixture "gen_test" called directly. Fixtures are not meant to be called directly,
but are created automatically when test functions request them as parameters.
See https://docs.pytest.org/en/latest/fixture.html for more information about fixtures, and
https://docs.pytest.org/en/latest/deprecations.html#calling-fixtures-directly about how to update your code.

Is there any workaround ? Ideally I would like to avoid passing a regular function (unless for some reason it's bad behavior) because my fixture is dependent on another fixture

@Overdrivr
Copy link
Author

Any updates on this ? Using pytest major version 4 breaks builds instead of just raising a warning.
Related: pytest-dev/pytest#3950

@wolever
Copy link
Owner

wolever commented Feb 5, 2019

Thanks for the report! I'll take a look into this when I get some cycles and report back. In the meantime, the larger issue with pytest 4 is here: #34

I'll post an update when I've figured out how to get this working.

@jatinp1
Copy link

jatinp1 commented Oct 15, 2019

Calling fixture directly feature is deprecated in 4.0 version. Install 3.10.0 version. It will work

@asher-dev
Copy link

asher-dev commented Nov 19, 2019

I don't think there's an easy fix for this, but it can be done with a call to request.getfixturevalue(fixture_name) on the fixture request object (which is a parameter for fixtures) as long as there's a fixture context to call it from.

EDIT: I just looked again at what you're trying to do, and I think it will not be possible to use a fixture in this way with pytest 4. In order to parameterize the test, the parameters must be available outside of the test context -- but fixtures in pytest 4 cannot be run outside of a test context. If you need this to be a fixture because you use it as a fixture elsewhere, consider a layer of indirection (i.e. have the fixture call a non-fixture function that produces these values).

@wolever
Copy link
Owner

wolever commented Nov 19, 2019

Hey folks - after consultation with @asher-dev, I don't believe this is a feature parameterized should support.

As mentioned, this appears to be an abuse (or, at least, misuse) of pytest fixtures as implemented in pytest4… so it might be possible to hack around, but I don't believe there's a strong usecase.

If you believe you have a usecase that is inline with "correct" pytest fixture usage, please comment here; I'm happy to reopen the issue if my understanding is faulty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants