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

scope=session fixtures setup/teardown multiple times #3470

Closed
cielavenir opened this issue May 14, 2018 · 7 comments
Closed

scope=session fixtures setup/teardown multiple times #3470

cielavenir opened this issue May 14, 2018 · 7 comments
Labels
topic: fixtures anything involving fixtures directly or indirectly type: question general question, might be closed after 2 weeks of inactivity

Comments

@cielavenir
Copy link

__init__.py

import pytest
import inspect
@pytest.fixture(scope="session")
def F(request):
  print('setup')
  yield inspect.stack()[0]
  print('teardown')

test_1.py

from . import F
def test_1(F):
  print(F)

test_2.py

from . import F
def test_2(F):
  print(F)

this pytest tree shows:

test_1.py setup
(<frame object at 0x7ff2c97fa730>)
.                                                                                                                                                              
test_2.py setup
(<frame object at 0x7ff2c97faaa0>)
.teardown
teardown

I expect setup/teardown only once, since there are only one fixture definition.

If this is because global session fixtures are gathered multiple times, I think this can be fixed by checking module name in gathering.

@cielavenir
Copy link
Author

@pytestbot
Copy link
Contributor

GitMate.io thinks possibly related issues are #1878 (session scoped fixtures get executed multiple times. I suspect I am using pytest wrong), #1738 (Have a way to force session scope teardown ), #583 (Add a class example for Setup/TearDown with fixtures), #3094 (Use fixtures to implement xUnit setup/teardown ), and #503 (Session fixtures invoked multiple times with xdist).

@cielavenir
Copy link
Author

cielavenir commented May 14, 2018

I tried to patch FixtureManager like:

            faclist = self._arg2fixturedefs.setdefault(name, [])
            if fixture_def.has_location:
                if fixture_def.scope!='session' or all(fdef.func!=fixture_def.func for fdef in faclist):
                    faclist.append(fixture_def)

and now test_2.py cannot find fixture F. It looks like I really have to put fixtures in conftest.py. Sorry to have bothered you (you can close this)

@cielavenir
Copy link
Author

looks like patching also baseid works...

            faclist = self._arg2fixturedefs.setdefault(name, [])
            if fixture_def.has_location:
                if fixture_def.scope=='session':
                    fixture_def.baseid=''
                if fixture_def.scope!='session' or all(fdef.func!=fixture_def.func for fdef in faclist):
                    faclist.append(fixture_def)

@RonnyPfannschmidt
Copy link
Member

importation counts as extra definition - so this is a user error

there is a plan to warn on multiple imports of the same fixture (as one issupposed to use a plugin/conftest

@RonnyPfannschmidt RonnyPfannschmidt added type: question general question, might be closed after 2 weeks of inactivity topic: fixtures anything involving fixtures directly or indirectly labels May 14, 2018
@cielavenir
Copy link
Author

fixed locally as https://github.com/cielavenir/pytest-session-fixture-globalize
this can be closed

@RonnyPfannschmidt
Copy link
Member

@cielavenir please document that this plugin may break even for minor or patch pytest releases ^^ its dabbling with the internals

kaczmarj added a commit to ReproNim/neurodocker that referenced this issue Apr 2, 2021
This introduces a race condition when using pytest-xdist with multiple
processes. There does not seem to be a good way of running the cleanup
once, even when using multiple processes. Related to
pytest-dev/pytest#3470
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: fixtures anything involving fixtures directly or indirectly type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

3 participants