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

pytest_runtest_setup is calling teardown code #4038

Closed
afrobbins805 opened this issue Sep 25, 2018 · 3 comments
Closed

pytest_runtest_setup is calling teardown code #4038

afrobbins805 opened this issue Sep 25, 2018 · 3 comments
Labels
topic: fixtures anything involving fixtures directly or indirectly type: refactoring internal improvements to the code

Comments

@afrobbins805
Copy link

afrobbins805 commented Sep 25, 2018

Dear All, here is my bug report on pytest behavior where the pytest_runtest_setup calls teardown code which should be called by the pytest_fixture_teardown hook.

Environment: [ pytest 3.8.1, pluggy 0.7.1, python 3.5.4, py 1.6.0, os 'windows 10 64-bit']

Conditions:

  • A session fixture is parameterized. (Fixture 1)
  • There is a another fixture of scope: [session, module, or class] (NOT function) that takes the parameterized session fixture as an argument (Fixture 2)
  • There are multiple tests which the second fixture applies to, per session parameter

Behavior:

  • The teardown code from Fixture 2 will be called by the pytest_runtest_setup hook of the next item's call.
  • The pytest_runtest_makereport hook is always called before the teardown code called by pytest_runtest_setup
  • Function scope fixture teardown code and higher scoped teardown code from a fixture that is not session parameterized is called from the pytest_fixture_teardown hook.

Comments:
The behaviour that I would prefer and suggest is that all teardowns are called from within the pytest_fixture_teardown hook and not from the pytest_runtest_setup hook for the next item. I will look into changes that would make this possible.

P.S. First time posting on here. Thank you to everyone who has made pytest a useful tool to me.

Example Code:

# conftest.py
def pytest_generate_tests(metafunc):
    print('pytest generate test called')
    config_dict_list = [{'a':0},{'a':1}]
    metafunc.parametrize("config_dict",config_dict_list,indirect=True,scope='session')

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_setup(item):
    print('pytest_runtest_setup start')
    yield
    print('pytest_runtest_setup end')

@pytest.hookimpl(trylast=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()
    print('runtest_makereport={}'.format(str(rep.when)))
    print('runtest_makereport={}'.format(item))

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_teardown(item, nextitem):
    print('pytest_runtest_teardown start')
    yield
    print('pytest_runtest_teardown end')
# test_example.py
import pytest

@pytest.fixture(scope='session',autouse=True)
def config_dict(request):
    print('config_dict setup')
    yield request.param
    print('config_dict teardown')

@pytest.fixture(scope='module')
def session_module_fixture(config_dict):
    print('session_module_fixture setup')
    yield
    print('session_module_fixture teardown')

@pytest.fixture(scope='class')
def non_session_class_fixture():
    print('non_session_class_fixture setup')
    yield
    print('non_session_class_fixture teardown')

@pytest.fixture(scope='function')
def function_fixture(config_dict):
    print('function_fixture setup')
    yield
    print('function_fixture teardown')

class TestExampleClass():
    def test_example_method(self,non_session_class_fixture):
        print('test_example_method call')

def test_example_function(function_fixture,session_module_fixture):
    print('test_example_function call')

def test_example_function2(function_fixture,session_module_fixture):
    print('test_example_function2 call')
@afrobbins805
Copy link
Author

Update: It seems that if the scope objects are used to make the teardown decisions within the _teardown_towards method (_pytest.runner.py line 342), there must be a scope object created per fixture of scope higher than function that is parameterized. By scope objects, I mean the objects created by the classes: Module and Class from _pytest/python.py and Session from _pytest/main.py. Any thoughts on this?

@Zac-HD Zac-HD added type: question general question, might be closed after 2 weeks of inactivity type: refactoring internal improvements to the code topic: fixtures anything involving fixtures directly or indirectly labels Oct 20, 2018
@Zac-HD Zac-HD removed the type: question general question, might be closed after 2 weeks of inactivity label Dec 9, 2018
@bluetech
Copy link
Member

bluetech commented Nov 3, 2020

Hi @afrobbins805,

This issue is hard to understand, but I believe what you say (teardowns running from setups of next item) doesn't happen anymore. Let's us know if not.

@bluetech bluetech closed this as completed Nov 3, 2020
@bluetech
Copy link
Member

bluetech commented Nov 3, 2020

Or maybe it's a duplicate of #4871.

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: refactoring internal improvements to the code
Projects
None yet
Development

No branches or pull requests

3 participants