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

Fixtures defined in case files not found #193

Closed
marvinpfoertner opened this issue Mar 24, 2021 · 5 comments
Closed

Fixtures defined in case files not found #193

marvinpfoertner opened this issue Mar 24, 2021 · 5 comments

Comments

@marvinpfoertner
Copy link

marvinpfoertner commented Mar 24, 2021

It seems that fixtures defined in case files can not be found.

Seems to be related to #174.
However, the workarounds/solutions described in #174 don't really work for my usecase, since I want to keep the fixture local to my cases file.

Example

File test_foo_cases.py:

import pytest_cases


@pytest_cases.fixture
def value():
    return 1, 2


def case_two_positive_ints(value):
    """ Inputs are two positive integers """
    return value

File test_foo.py:

import pytest_cases


@pytest_cases.parametrize_with_cases("x")
def test_foo(x):
    assert x is not None

When running test_foo, I get

________________ ERROR at setup of test_foo[two_positive_ints] _________________
file .../test_foo.py, line 4: source code not available
file <makefun-gen-10>, line 1: source code not available
file <makefun-gen-8>, line 1: source code not available
E       fixture 'values' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, cov, doctest_namespace, monkeypatch, no_cover, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, test_foo_x, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory, two_positive_ints
>       use 'pytest --fixtures [testpath]' for help on them.
@smarie
Copy link
Owner

smarie commented Mar 24, 2021

THanks @marvinpfoertner ! From the documentation indeed it is a bot ambiguous.

From what I remember, defining a fixture in a non test file does not work. I'll check this now to be sure there is no easy workaround

@smarie
Copy link
Owner

smarie commented Mar 24, 2021

@marvinpfoertner which version of pytest-cases are you using ? Indeed when I run the same example as you do, I get a different error. This might help me understanding if there was a regression.

@marvinpfoertner
Copy link
Author

marvinpfoertner commented Mar 24, 2021

@smarie Thanks for responding so quickly! I'm running version 3.2.1. But I also just noticed that I pasted the wrong code snippet for test_foo_cases.py. I just updated the issue. Now the error message should match the code snippet.

@smarie smarie closed this as completed in 97e155c Mar 24, 2021
@smarie
Copy link
Owner

smarie commented Mar 24, 2021

Perfect. So I confirm, from there are only two ways in pytest to share a fixture between two modules (in your case the module containing the cases contains the fixture on one hand, and the module using @parametrize_with_cases on the other hand requires the fixture) :

  • either the module "consuming" it (the module with @parametrize_with_cases) should import it explicitly

  • or the conftest.py file should import it explicitly, as proposed in this blog post

See also pytest doc

Still, I managed to create an experimental flag @parametrize_with_cases(import_fixtures=True) that you can set to import all fixtures from the cases definition module if this is needed.

Let me know if this new feature, available in the 3.4.0 release this evening, works for you. Otherwise feel free to reopen this ticket

@marvinpfoertner
Copy link
Author

@smarie Thanks a lot! I'll try it out and keep you posted.

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

2 participants