Skip to content

Enable fixture dynamic scoping during test run #6101

Open

Description

@rnetser

pytest version 5.2.2

Tried to use dynamic scopes for fixtures. However, the scope definition is executed only once - during the fixture definition.
We would like to have option to control fixture scope during test run.
This will allow code re-use in tests which need the code in different scope.
For example: set up a docker - one test needs it in function level and a separate test needs it in module level.

@pytest.fixture()
def some_fixture():
    pass

# test_method.py
@pytest.mark.scope('some_fixture', 'module')
def test_method(some_fixture):
    pass

# test_function.py
@pytest.mark.scope('some_fixture', 'function')
def test_method(some_fixture):
    pass

Activity

  1. added
    topic: fixturesanything involving fixtures directly or indirectly
    type: proposalproposal for a new feature, often to gather opinions or design the API around the new feature
    on Oct 30, 2019
  2. aklajnert commented on Nov 12, 2019

    @aklajnert
    Contributor

    How about extracting fixture body into a separate function, and define two fixtures with different scopes that will call the function?

    def fixture_body():
        pass
    
    
    @pytest.fixture(scope="function")
    def some_fixture_module():
        fixture_body()
    
    
    @pytest.fixture()
    def some_fixture_function():
        fixture_body()
    
    
    # test_method.py
    def test_method(some_fixture_module):
        pass
    
    
    # test_function.py
    def test_method(some_fixture_function):
        pass
  3. RonnyPfannschmidt commented on Nov 12, 2019

    @RonnyPfannschmidt
    Member

    @rnetser we do have plans to enable fixtures to be available at multiple scopes (i don't have the issue number ready),
    we don't yet have a plan to allow the test to do the detailed picking as its not yet clear how the concepts integrate

    @aklajnert what you outlined is the current workaround, thanks for taking the time to put it together

  4. rnetser commented on Nov 12, 2019

    @rnetser
    Author

    @aklajnert thanks, this is what we have implemented as a workaround.

    @RonnyPfannschmidt is there a timeline for this?

  5. RonnyPfannschmidt commented on Nov 12, 2019

    @RonnyPfannschmidt
    Member

    @rnetser unfortunately there isn't, when we tried to add the feature for pytest 3.0 in the pytest sprint in Freiburg, we learned that the current conceptual layout of the fixture system makes this feature error prone (@nicoddemus didnt spare efforts to make it work then, however one unexpected bug after another turned up, as such we had to stop and rethink)

    so far we haven't found the time to find a conceptual layout that makes this feature solid

  6. victormaricato commented on Apr 1, 2021

    @victormaricato

    @rnetser @nicoddemus now with pytest 6.2.2, do you think this feature is somehow more tangible? Can you share your thoughts on the implementation?

  7. RonnyPfannschmidt commented on Apr 1, 2021

    @RonnyPfannschmidt
    Member

    @maricatovictor we didnt touch the relevant fixture internals as of now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytype: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions