Skip to content

Fixture created for parametrized case not visible to test function in different module #174

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
plammens opened this issue Dec 18, 2020 · 1 comment
Labels
bug Something isn't working has_workaround

Comments

@plammens
Copy link
Contributor

plammens commented Dec 18, 2020

As discussed in #158 (in particular #158 (comment) and #158 (comment)), when a fixture is created for a parametrized case, if that case is used in a test that lies in a separate module, the fixture won't be visible and test collection will fail.

Related to #170, #169

Example

In the file test_foo_cases.py:

import pytest_cases



class XCases:
    @staticmethod
    def case_foo():
        return 1


class YCases:
    @staticmethod
    @pytest_cases.parametrize_with_cases("x", XCases.case_foo)
    def case_foo(x):
        return x


class ZCases:
    @staticmethod
    @pytest_cases.parametrize_with_cases("y", YCases.case_foo)
    def case_foo(y):
        return y

In test_foo.py:

import pytest_cases
import test_foo_cases


@pytest_cases.parametrize_with_cases("y", test_foo_cases.ZCases.case_foo)
def test_foo(y):
    pass

Trying to run test_foo will result in an error during collection:

_______________________ ERROR at setup of test_foo[foo] _______________________
file .../test_foo.py, line 5: source code not available
file <makefun-gen-14>, line 1: source code not available
file ...
E       fixture 'case_foo_y' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, foo, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, test_foo_y, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

Workarounds

Possible workarounds are:

  • Import everything from the cases module to ensure all dynamically generated fixtures are visible in the test module too: from test_foo_cases import *
  • Selectively import the dynamically generated fixture in the test module: case_foo_y = test_foo_cases.case_foo_y. This relies on knowing the name of the generated fixture.

Possible solutions

  • Dynamically add the fixture to all modules that reference the case too?
  • Instead of adding the fixture to the cases module, add it to the closest conftest.py available?
@smarie
Copy link
Owner

smarie commented Dec 18, 2020

Nice workaround from test_foo_cases import * ! I did not think it would work but actually there is no reason it shouldnt

Another possible solution is : stop creating hidden fixtures in @parametrize (#170). A bit ambitious/challenging but not impossible.

@plammens plammens changed the title Fixture creation for parametrized case not visible to test function in different module Fixture created for parametrized case not visible to test function in different module Dec 18, 2020
@smarie smarie added bug Something isn't working has_workaround labels Apr 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working has_workaround
Projects
None yet
Development

No branches or pull requests

2 participants