-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
pytest_generate_tests
conflicts with conftest.py
fixtures
#2726
Comments
@vlcinsky thanks a lot for all the effort you put into creating a repository and documenting each case, we really appreciate it. |
@nicoddemus This is my contribution to my beloved testing framework. Good quality issue description is rather easy comparing to reading pytest internals what I gave up being happy, others take care of it. |
Hi,
I have a fake fixture function in @pytest.fixture(
params=[
"samples/video1.mp4",
"samples/video2.mp4",
"samples/video3.mp4",
]
)
def video_input(request):
return request.param
def pytest_addoption(parser):
parser.addoption(
"--video_path",
action="append",
default=[],
help="list of video path to pass to test functions",
) And def get_video_duration(video):
return 20
def pytest_generate_tests(metafunc):
if "video_input" in metafunc.fixturenames:
metafunc.parametrize("video_input", metafunc.config.getoption("video_path"))
def test_get_dynamic_video_duration(video_input):
video = video_input
assert get_video_duration(video) == 20 Here is the $ pytest -v --no-header --video_path="samples/video4.mp4" tests/ex6/test_hook.py
=========================================================================================== test session starts ===========================================================================================
collected 0 items / 1 error
================================================================================================= ERRORS ==================================================================================================
____________________________________________________________________________________ ERROR collecting ex6/test_hook.py ____________________________________________________________________________________
.venv/lib/python3.10/site-packages/_pytest/runner.py:341: in from_call
result: Optional[TResult] = func()
.venv/lib/python3.10/site-packages/_pytest/runner.py:372: in <lambda>
call = CallInfo.from_call(lambda: list(collector.collect()), "collect")
.venv/lib/python3.10/site-packages/_pytest/python.py:534: in collect
return super().collect()
.venv/lib/python3.10/site-packages/_pytest/python.py:455: in collect
res = ihook.pytest_pycollect_makeitem(
.venv/lib/python3.10/site-packages/pluggy/_hooks.py:433: in __call__
return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
.venv/lib/python3.10/site-packages/pluggy/_manager.py:112: in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
.venv/lib/python3.10/site-packages/_pytest/python.py:271: in pytest_pycollect_makeitem
return list(collector._genfunctions(name, obj))
.venv/lib/python3.10/site-packages/_pytest/python.py:498: in _genfunctions
self.ihook.pytest_generate_tests.call_extra(methods, dict(metafunc=metafunc))
.venv/lib/python3.10/site-packages/pluggy/_hooks.py:489: in call_extra
return self._hookexec(self.name, hookimpls, kwargs, firstresult)
.venv/lib/python3.10/site-packages/pluggy/_manager.py:112: in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
.venv/lib/python3.10/site-packages/_pytest/fixtures.py:1570: in pytest_generate_tests
metafunc.parametrize(
.venv/lib/python3.10/site-packages/_pytest/python.py:1347: in parametrize
newcallspec = callspec.setmulti(
.venv/lib/python3.10/site-packages/_pytest/python.py:1152: in setmulti
raise ValueError(f"duplicate {arg!r}")
E ValueError: duplicate 'video_input'
========================================================================================= short test summary info =========================================================================================
ERROR tests/ex6/test_hook.py - ValueError: duplicate 'video_input'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================================ 1 error in 0.41s ============================================================================================= |
i believe this can be summarized as parametrize working unconditionally, so params and generate_tests are "mutually exclusive" both are implemented in terms of invoking metafunc.parametrize fixing would to add support for either way would require overriding parametriting only if a parameter is not overridden |
a temporary workaround would be to remove the params on the fixture, and passing that value as default in generate_tests if no input was given |
Thanks for you prompt reply. In the Python testing with pytest, 2nd ed book, the example uses You can look at chapter 5 source code, Surprisingly the fixture code is not in If I put my fixture in another test file in the same folder than |
indeed, it works if the individual hooks/configurations only apply to specific test files if one moved all the implementations over to a conftest, |
I realized with this problem that I did not fully understand fixture loading / usage. In the book example, This is the same for I note that I should not use Thanks for your kind help ! |
Dedicated repository for exhibiting the problem and with detailed description see https://github.com/vlcinsky/pytest_generate_tests-conflicts
From "summary" in repository README.md
Duplicate values
As shown in
003_conftest_fixt1
, whenpytest_generate_tests
provided a value for test caseparameter and at the same time
conftest.py
provides a fixture with the same name, it wronglyattempts to define a test case call also with the value from
conftest.py
fixture.Expected behaviour is, that once a test function gets value for a parameter from
pytest_generate_tests
, then further collection of values for this parameter shall stop.Alternatively it shall resolve the duplication by giving the
pytest_generate_tests
parameter valuepreference.
Collection collects tests multiple times
As shown in
005_conftest_fixt2
, whenpytest_generate_tests
provides a value for a test caseparameter and at the same time in
conftest.py
there exist a fixture, which is dependent on anotherone, it results in collecting the same test call (having the same combination of parameter values)
multiple times, this time it goes around
Duplicate values
by generating new test id usingparameter value from
conftest.py
fixture.Expected behaviour is the same as described in "Duplicate values" above.
The text was updated successfully, but these errors were encountered: