Skip to content

pytest 3.3.0 immutable fixture params #2959

Closed

Description

@popravich

Before v3.3.0 it was possible to mutate fixture parameters, since v3.3.0 params gets "frozen"
(I guess its introduced with this commit 07b2b18)

I was using this feature to setup fixture parameters based on command-line options:

BINS = []

@pytest.fixture(scope='session', params=BINS, ids=...)
def server_bin(request):
    return request.param

@pytest.fixture(scope='session')
def run_server(server_bin):
    pass # start application

def pytest_configure(config):
    BINS[:] = config.getoption('--server')[:]

So my questions:

  • Is this new behavior intended or its a bug?
  • And if its intended what is new way to make dynamic parameters?

Activity

  1. added
    type: regressionindicates a problem that was introduced in a release which was working previously
    on Nov 28, 2017
  2. popravich commented on Nov 28, 2017

    @popravich
    Author
  3. RonnyPfannschmidt commented on Nov 28, 2017

    @RonnyPfannschmidt
    Member

    its neither intended, not was it a bug

    we simply didn't fall over because users would mutate random aliased objects that would then end up in the internals

    the correct way to make fixtures dynamic is something like this:

    def pytest_configure(config):
      class DynamicFixturePlugin(object):
         @pytest.fixture(scope='session', params=config.getoption('--servers'), ids=...)
         def server_bin(request):
            return request.param
        config.pluginmanager.register(DynamicFixturePlugin(), 'server-bin-fixture')
  4. popravich commented on Nov 28, 2017

    @popravich
    Author

    Ok, thanks,
    I will try it

  5. nicoddemus commented on Nov 28, 2017

    @nicoddemus
    Member

    Do we still consider this regression?

  6. RonnyPfannschmidt commented on Nov 28, 2017

    @RonnyPfannschmidt
    Member

    @nicoddemus it practically is a regression, but its one i'd like to not fix since fixing it makes things worse in the internals again

  7. nicoddemus commented on Nov 28, 2017

    @nicoddemus
    Member

    @RonnyPfannschmidt I agree, doesn't seem like something we want to actually fix, at least not in its current form where it was working by accident rather than by design.

    @popravich when you can please let us know if the workaround worked for you.

  8. argaen commented on Nov 29, 2017

    @argaen

    I just found out about this today. Is it worth it to at least add a disclaimer somehow in the CHANGELOG? Our deployment to PROD was skipping some tests without us even noticing so I believe its something worth to comment.

  9. popravich commented on Nov 29, 2017

    @popravich
    Author

    @nicoddemus, the suggested code works with a tiny fix

    def pytest_configure(config):
      class DynamicFixturePlugin(object):
         @pytest.fixture(scope='session', params=config.getoption('--servers'), ids=...)
    -     def server_bin(request):
    +     def server_bin(self, request):
            return request.param
        config.pluginmanager.register(DynamicFixturePlugin(), 'server-bin-fixture')

    And I'm +1 to @argaen about some notes in CHANGELOG.

  10. added a commit that references this issue on Nov 29, 2017
    fdfc194
  11. nicoddemus commented on Nov 29, 2017

    @nicoddemus
    Member

    @argaen and @popravich agree, thanks for the suggestion: #2980

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

    type: regressionindicates a problem that was introduced in a release which was working previously

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions