question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
RonnyPfannschmidtcommented, Mar 29, 2018

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

2reactions
vanderlooscommented, Mar 30, 2018

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Access autouse fixture without having to add it to the method ...
Your log function is not a fixture. Treat it the same as any other library routine. Eg: add it to a common module...
Read more >
How to use fixtures — pytest documentation
We can make a fixture an autouse fixture by passing in autouse=True to ... Possible values for scope are: function , class ,...
Read more >
pytest fixtures: explicit, modular, scalable
Software test fixtures initialize test functions. They provide a fixed baseline so that tests execute reliably and produce consistent, repeatable, results.
Read more >
Using autouse for Fixtures That Always Get Used - O'Reilly
​, ​import​ pytest ; ​, ​import​ time ; ​ ; ​ ; ​, @pytest.fixture(autouse=True, scope=​'session'​).
Read more >
How to Disable Autouse Fixtures in pytest - miguendes's blog
They are a special type of fixture that gets invoked automatically, and its main use case is to act as a setup/teardown function....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found