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

Override class-level fixture parametrization with method-level parametrization #9175

Open
jamesbraza opened this issue Oct 7, 2021 · 2 comments
Labels
topic: fixtures anything involving fixtures directly or indirectly type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@jamesbraza
Copy link

What's the problem this feature will solve?

Please see the below Python 3.8 code snippet. It involves:

  • Fixture being indirectly parametrized
    • Class parametrization: idea is this parameterization ("b") applies to all tests
    • Method parameterization: idea is this parameterization ("a") applies to just a particular method
from typing import Any

import pytest

@pytest.fixture
def some_fixture(request) -> Any:
    assert "a" in request.param  # Only passes for test_something
    assert "b" in request.param  # Always passes

# I apply to all test methods
@pytest.mark.parametrize("some_fixture", [{"b": "b"}], indirect=True)
class TestMultiParameterization:

    # I apply to just this one test method
    @pytest.mark.parametrize("some_fixture", [{"a": "a"}], indirect=True)
    def test_something(self, some_fixture: Any) -> None:
        pass

Running the test_something method via pytest==6.2.5, it will spit out the following error:

E   ValueError: duplicate 'some_fixture'

Clearly, pytest rejects the duplicated parameterization.

Describe the solution you'd like

I would like if one could somehow "stitch together" parameterization from class-level parameterizations and method-level parameterizations.

The main benefit would be increased flexibility in parameterization of test fixtures. I am not sure how this could be accomplished in practice, though I do think it'd be useful functionality.

Alternative Solutions

I have not tried any alternate solutions.

Additional context

@Zac-HD Zac-HD added topic: fixtures anything involving fixtures directly or indirectly type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature labels Oct 8, 2021
@RonnyPfannschmidt
Copy link
Member

the reduced example makes the intended feature absolutely unclear

please provide an example that indicates why/how this adds value, as the minimal example you posted seems very confusing for me and will absolutely not sail as is

@RonnyPfannschmidt RonnyPfannschmidt added the status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity label Oct 12, 2021
@jamesbraza
Copy link
Author

Yes, in hindsight it was not a very clear example, my apologies. Try this one:

import pytest


class Foo:
    def __init__(self, arg: str):
        self.arg = arg


@pytest.fixture
def foo(request) -> Foo:
    return Foo(arg=request.param)


# Note: this parameterization applies to all test methods
@pytest.mark.parametrize("foo", ["hi"], indirect=True)
class TestParameterizationFromClassAndMethod:

    # There's many tests like me that all want to use the class's parameterization
    def test_with_defaults(self, foo: Foo) -> None:
        assert foo.arg == "hi"

    # I want to override the class's parameterization with a different object
    @pytest.mark.parametrize("foo", ["hello"], indirect=True)
    def test_with_override(self, foo: Foo) -> None:
        # This test method will error out: ValueError: duplicate 'foo'
        assert foo.arg == "hello"

It involves:

  • Parameterization on test class: applies to all test methods
  • Parameterization on test method: applies to just that particular test method
    • In this case, the test method's parameterization overrides the test class's parameterization

Does this make more sense? At its core, this feature request is about having test methods be able to override parameterizations from test classes.

@Zac-HD Zac-HD changed the title Ability to indirectly parametrize fixture from both class and test Override class-level fixture parametrization with method-level parametrization Nov 29, 2021
@Zac-HD Zac-HD removed the status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity label Nov 29, 2021
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: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

3 participants