Fixture with `scope='session'` and `autouse=True` not being executed with `-k`
See original GitHub issueI have a fixture that is being executed if I run pytest
but not if I run pytest -k path/to/test_something.py
.
The project directory structure looks like
. # project root
app/ # django app
app/tests/
app/tests/conftest.py
app/tests/test_something.py
In app/tests/conftest.py
I have something like this:
import pytest
print 'OHAI'
@pytest.fixture(scope='session', autouse=True)
def create_directories(request):
print 'CREATE'
# creates some fixture directories that are needed by tests
If I run pytest
from the project’s root, this works as expected - the create_directories
fixture is getting executed. If I run pytest -k app/tests/test_something.py
, it does not execute the fixture. However, I can tell that conftest.py is being evaluated because the print statement is executed. Furthermore, if I put create_directories(None)
in the file below the function definition, the function is executed (which also confirms that the function is not erroring out).
The fixture is shown in the output of both pytest --fixtures
and pytest --fixtures -k app/tests/test_something,py
.
I am using python 2.7.12 on mac OS 10.13.1 with a virtualenv managed by pipenv with pytest 3.3.0. I can’t share the full pip list, but I can probably confirm specific packages. I do have mock, coverage, pytest-django, pytest-cov, and pytest-env installed.
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (5 by maintainers)
i suspect @vanderloos is missunderstanding autouse
autouse does not magically make a variable availiable autouse means set it up even if its not listed in parameters
if you want to locally use it, add it to the parameters so pytest can pass it to you
Well, looks like you were right @RonnyPfannschmidt, @nicoddemus I misunderstood the purpose of ‘autouse’. It is for “always run the fixture code” and is used for setup/teardown puposes. And for my test-data-preparation fixture, where the fixture returns a value, I anyway need to pass the fixture name to the test input params to have it locally. If I don’t pass the fixture name, its code is executed but the result is returned to nowhere. Thanks for the help!