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

Fixture applied more than once - when running multiple tests #126

Closed
stephenlagree opened this issue Aug 17, 2020 · 3 comments · Fixed by #127
Closed

Fixture applied more than once - when running multiple tests #126

stephenlagree opened this issue Aug 17, 2020 · 3 comments · Fixed by #127

Comments

@stephenlagree
Copy link

stephenlagree commented Aug 17, 2020

It seems I can only use my set of cases with a fixture for one test or else I get the following error

"fixture is being applied more than once to the same function" E ValueError: fixture is being applied more than once to the same function

Here is code that reproduces:

import pytest
from pytest_cases import parametrize_with_cases


@pytest.fixture()
def dependent_fixture():
    return 1


def case_requirement_1(dependent_fixture):
    return 1 + dependent_fixture


def case_requirement_2(dependent_fixture):
    return 2 + dependent_fixture


@parametrize_with_cases("my_requirement",cases=".", prefix="case")
def test_functionality(my_requirement):
    print(my_requirement)


@parametrize_with_cases("my_requirement",cases=".", prefix="case")
def test_functionality_again(my_requirement):
    print(my_requirement)

If I remove the second test, it works fine. Also, if I make the cases not depend on a fixture, it works fine.
It seems like it's including the fixture too many times when the decorator is used more than once.

@smarie
Copy link
Owner

smarie commented Aug 18, 2020

Thanks very much for reporting ! I'll have a look

@smarie
Copy link
Owner

smarie commented Aug 18, 2020

This is related to the way I handle cases requiring fixtures as of today: I create one fixture for each such case, and I register this fixture in the collection host (module, class) of the test function/fixture parametrized with the cases. This was not entirely bullet proof as you discovered: I was not properly creating a copy (hence the error message you receive, saying that the case function that was already turned into a fixture, is once again turned into a fixture).

This requires some major refactoring to handle all 4 situations (case in module/class, test in module/class) and because in python it is quite difficult to detect the actual host (class or module) of a function ; but I made progress today. I'll keep you posted.

smarie pushed a commit that referenced this issue Aug 19, 2020
…me function` when two functions parametrized with the same cases were sitting in the same file. Fixed #126
smarie pushed a commit that referenced this issue Aug 19, 2020
@smarie
Copy link
Owner

smarie commented Aug 19, 2020

This was a hard one :) It seems to be fixed in 2.2.0
Please feel free to reopen if it is not fixed ! Thanks again for reporting @stephenlagree

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