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

no parameter defined for test error when overriding a fixture that requests a parametrized fixture #11075

Open
azmeuk opened this issue Jun 3, 2023 · 0 comments
Labels
topic: fixtures anything involving fixtures directly or indirectly

Comments

@azmeuk
Copy link
Member

azmeuk commented Jun 3, 2023

python 3.11 - pytest 7.3.1 - archlinux
Related issues #1953 #2043

The situation is a bit convoluted: if a fixture requests a parametrized fixture, and is overridden with different params, then it raises a The requested fixture has no parameter defined for test error.

import pytest


@pytest.fixture(params=["foo"])
def fixture_foo():
    yield None


@pytest.fixture
def fixture_bar(fixture_foo):
    yield None


class TestFoobar:
    @pytest.fixture
    def fixture_bar(self, fixture_bar):
        yield None

    def test_foobar(self, fixture_bar):
        assert True
===================================================== test session starts =====================================================
platform linux -- Python 3.11.3, pytest-7.3.1, pluggy-1.0.0
rootdir: ...
collected 1 item

test_foobar.py E                                                                                                        [100%]

=========================================================== ERRORS ============================================================
__________________________________________ ERROR at setup of TestFoobar.test_foobar ___________________________________________
The requested fixture has no parameter defined for test:
    test_foobar.py::TestFoobar::test_foobar

Requested fixture 'fixture_foo' defined in:
test_foobar.py:5

Requested here:
.../lib/python3.11/site-packages/_pytest/fixtures.py:678
=================================================== short test summary info ===================================================
ERROR test_foobar.py::TestFoobar::test_foobar
====================================================== 1 error in 0.02s =======================================================

I encountered this in a real-life project. I simplified all that I could to make an example demonstrating this behavior. Obviously this example does not seem very relevant, but in my real usecase I do need the overriding fixture_bar to have a param to the main fixture_bar.

The error won't happen if:

  • fixture_foo is not parametrized or
  • the overriding fixture_bar also have a fixture_foo param

It took me a while to find how to fix the issue, but this second workaround is simple enough. However I could not find any documentation about this.

I suggest to either fix this behavior, or document it.

What do you think?

@Zac-HD Zac-HD added the topic: fixtures anything involving fixtures directly or indirectly label Jun 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: fixtures anything involving fixtures directly or indirectly
Projects
None yet
Development

No branches or pull requests

2 participants