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

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

Open
AlbertoRFer opened this issue Mar 6, 2025 · 0 comments
Open

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

AlbertoRFer opened this issue Mar 6, 2025 · 0 comments

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant