Pytest: Fixture with `scope='session'` and `autouse=True` not being executed with `-k`

Created on 5 Dec 2017  路  8Comments  路  Source: pytest-dev/pytest

I 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.

Most helpful comment

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

All 8 comments

This is working for me after updating to 3.3.1.. whatever you did to fix it, thanks!

That's strange, doesn't seem like this could have been fixed by any of the fixes according to the CHANGELOG.

Oh well, glad at least your problem went away. Feel free to reopen this if it resurfaces.

I'm now experiencing the same problem with autouse='True' regardless of the scope.
I'm running Python 3.6.4, pytest-3.4.1 under conda virtual environment. Starting like this:
source activate py3
python -m pytest -v $PYTHON_TEST_DIR
I have my fixture in _conftest.py_:

import os

@pytest.fixture(scope='session', autouse=True)
def is_windows():
    if os.name == 'nt':
        return True
    else:
        return False

If I explicitly put the fixture name into the test input params, it works fine. But when I want the fixture to get autoused, I get NameError: name 'is_windows' is not defined inside the test.

Hmm NameError: name 'is_windows' is not defined seems like a different error. Can you post the code of one the tests which show the problem?

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

Yep I'm suspecting the same thing, that's why I asked to see some code; but I think your suspicion looks like it is on the spot.

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!

@vanderloos thanks for following up, i'm happy we could help you resolve that miss-understanding

Was this page helpful?
0 / 5 - 0 ratings