pytest
pytest copied to clipboard
over-riden/extended fixtures that self-depend don't pass over dependend fixtures for parameterization
When I have the following situation:
- A fixture
mainthat uses another fixtureparam - A test class
TestFoothat:- overrides the
mainfixture with a fixture that uses the "parent" fixture - has a test
test_foothat uses themainfixture and is marked to be parametrised overparam
- overrides the
pytest gives me the error "Failed: In test_foo: function uses no argument 'param'".
I was expecting the "parent" main fixture to receive the parametrised param value.
The code:
from pytest import fixture, mark
@fixture
def param() -> int:
return 1
@fixture
def main(param: int) -> int:
return sum(range(param + 1))
class TestFoo:
@fixture
def main(self, main: int) -> int:
return main
@mark.parametrize("param", [2])
def test_foo(self, main: int) -> None:
assert main == 3
The error:
$ pytest
======================================== test session starts =========================================
platform linux -- Python 3.12.2, pytest-8.0.2, pluggy-1.4.0
rootdir: /home/rob/tmp/py
collected 0 items / 1 error
=============================================== ERRORS ===============================================
___________________________________ ERROR collecting test_class.py ___________________________________
In test_foo: function uses no argument 'param'
====================================== short test summary info =======================================
ERROR test_class.py::TestFoo - Failed: In test_foo: function uses no argument 'param'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================================== 1 error in 0.05s ==========================================
pip list output:
$ uv pip list
Package Version
--------- -------
iniconfig 2.0.0
packaging 23.2
pluggy 1.4.0
pytest 8.0.2
OS: Fedora 39.
Add indirect=True
Add indirect=True
Hi @RonnyPfannschmidt. I tried this, as I think this is what you meant:
from pytest import fixture, mark
@fixture
def param() -> int:
return 1
@fixture
def main(param: int) -> int:
return sum(range(param + 1))
class TestFoo:
@fixture
def main(self, main: int) -> int:
return main
@mark.parametrize("param", [2], indirect=True)
def test_foo(self, main: int) -> None:
assert main == 3
But I still get: ERROR test_class.py::TestFoo - Failed: In test_foo: function uses no fixture 'param'
Ok,this looks like a possible bug wrt fixture overrides
Please add the parameter to the second main fixture as well,
Please add the parameter to the second main fixture as well,
Yes, it works if the main fixture within TestFoo takes the param fixture but does not use it:
class TestFoo:
@fixture
def main(self, main: int, param: int) -> int:
return main
@mark.parametrize("param", [2])
def test_foo(self, main: int) -> None:
assert main == 3