Skip to content

How to change the fixture used in case functions. #360

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

Open
AlbertoRFer opened this issue Mar 6, 2025 · 1 comment
Open

How to change the fixture used in case functions. #360

AlbertoRFer opened this issue Mar 6, 2025 · 1 comment
Labels
has_workaround question Further information is requested

Comments

@AlbertoRFer
Copy link

I have several cases in a class, all of them have a fixture in common, lets call it fixture1 something like.

@pytest.fixture
def fixture1():
    return "dummy-fixture1"

class CaseFixture:
    def case_1(self, fixture1):
        return f"{fixture1}-case-1"

    def case_2(self, fixture1):
        return f"{fixture1}-case-2"

@parametrize_with_cases("case", cases=CaseFixture)
def test_fixture1(case):
    assert case in ["dummy-fixture1-case-1", "dummy-fixture1-case-2"]

Now for my next test function i could reuse the same cases in CaseFixture if the case functions took a different fixture than fixture1, for example fixture2. The naive way with a lot of code duplication would be:

@pytest.fixture
def fixture1():
    return "dummy-fixture1"

class CaseFixture1:
    def case_1(self, fixture1):
        return f"{fixture1}-case-1"

    def case_2(self, fixture1):
        return f"{fixture1}-case-2"

@pytest.fixture
def fixture2():
    return "dummy-fixture2"

class CaseFixture2:
    def case_1(self, fixture2):
        return f"{fixture2}-case-1"

    def case_2(self, fixture2):
        return f"{fixture2}-case-2"

@parametrize_with_cases("case", cases=CaseFixture1)
def test_fixture1(case):
    assert case in ["dummy-fixture1-case-1", "dummy-fixture1-case-2"]

@parametrize_with_cases("case", cases=CaseFixture2)
def test_fixture2(case):
    assert case in ["dummy-fixture2-case-1", "dummy-fixture2-case-2"]

Is there a way of reuse the cases instead? Something like passing the fixture to use from the test function to the Case class methods?

@smarie
Copy link
Owner

smarie commented May 6, 2025

Hi @AlbertoRFer , thanks for your interesting question !

It seems that you would like to de-couple the cases from the parameter of the cases.

My naive proposal would be to consider having cases that return callables, and have the "mix" of fixture + case done by the test itself

@parametrize_with_cases("case", cases=CaseFixture1)
def test_fixture1(case, fixture1):
    actual_case = case(fixture1)
    ...

@parametrize_with_cases("case", cases=CaseFixture2)
def test_fixture2(case, fixture2):
    actual_case = case(fixture2)
    ...

This can be very readable if you choose variable names that are intuitive. For example "data_creator", or "challenger", or "algorithm", or "function" instead of "case".

Note that you can do this in the test function or in another case as there is no limit in nesting.

@smarie smarie added has_workaround question Further information is requested labels May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has_workaround question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants