Repository navigation
Description
Miniconda Python 3.6.6, pytest 3.8.0 from conda-forge
STEREO_PATHS = ['stereo 1.wav', 'stereo 2.wav']
@pytest.mark.parametrize("path", STEREO_PATHS)
@pytest.mark.parametrize("cfg_factory", [list, dict]) # not actual params
@pytest.fixture(scope="module")
def stereo_cfg(path, cfg_factory): ... @pytest.mark.parametrize("path", STEREO_PATHS)
@pytest.mark.parametrize("cfg_factory", [n163_cfg, unrounded_cfg])
@pytest.fixture(scope="module")
def stereo_cfg(path, cfg_factory):
E fixture 'path' not found-
@pytest.fixture(params)does not allow me to parameterize one fixture with multiple orthogonal parameters (I want all 4 combinations ofpathandcfg_factory). -
I find the
pytest.fixture(params=[...])to be extremely inelegant and confusing compared to@pytest.mark.parametrize. This is because of the requirement that the function argument be namedrequest, the actual parameter be namedrequest.param(making the parameter name non-self-documenting and confusing at first).
maybe test functions can accept parametric parameters directly, while fixture functions can only accept fixtures as parameters? if so, that's a problem that should be solved.
Workaround: Boilerplate fixtures which wrap lists:
STEREO_PATHS = ['stereo 1.wav', 'stereo 2.wav']
@pytest.fixture(scope="module", params=STEREO_PATHS)
def path(request):
path = request.param
return Path('test_waves', path)
@pytest.fixture(scope="module", params=[list, dict])
def cfg_factory(request):
return request.param
@pytest.fixture(scope="module")
def stereo_cfg(path, cfg_factory): ...But this adds many boilerplate lines of code. I thought pytest was a boilerplate-free test system.
# packages in environment at /home/jimbo1qaz/miniconda3/envs/wavetable:
#
# Name Version Build Channel
atomicwrites 1.2.1 py36_0
attrs 18.2.0 py36h28b3542_0
blas 1.0 mkl
ca-certificates 2018.8.24 ha4d7672_0 conda-forge
certifi 2018.8.24 py36_1 conda-forge
click 6.7 py36_0
dataclasses 0.6 py_0 conda-forge
intel-openmp 2018.0.3 0
libedit 3.1.20170329 h6b74fdf_2
libffi 3.2.1 hd88cf55_4
libgcc-ng 8.2.0 hdf63c60_1
libgfortran-ng 7.3.0 hdf63c60_0
libstdcxx-ng 8.2.0 hdf63c60_1
mkl 2018.0.3 1
mkl_fft 1.0.4 py36h4414c95_1
mkl_random 1.0.1 py36h4414c95_1
more-itertools 4.3.0 py36_0
ncurses 6.1 hf484d3e_0
numpy 1.15.1 py36h1d66e8a_0
numpy-base 1.15.1 py36h81de0dd_0
openssl 1.0.2p h470a237_0 conda-forge
pip 10.0.1 py36_0
pluggy 0.7.1 py36h28b3542_0
py 1.6.0 py36_0
pytest 3.8.0 py36_0 conda-forge
python 3.6.6 hc3d631a_0
readline 7.0 h7b6447c_5
ruamel.yaml 0.15.65 py36h470a237_0 conda-forge
scipy 1.1.0 py36hfa4b5c9_1
setuptools 40.2.0 py36_0
six 1.11.0 py36_1
sqlite 3.24.0 h84994c4_0
tk 8.6.8 hbc83047_0
waveform-analysis 0.1 <pip>
wavetable 0.0.0 <pip>
wheel 0.31.1 py36_0
xz 5.2.4 h14c3975_4
zlib 1.2.11 ha838bed_2
Activity
New decorator `@pytest_fixture_plus` allows to use several `@pytest.m…
smarie commentedon Jan 8, 2019
I implemented a workaround
@pytest_fixture_plusinpytest-cases1.1.0. It behaves exactly like@pytest.fixtureexcept that in addition it supports@pytest.mark.parametrize:It has been tested against pytest 2.x and 3.x. It will remain available until this issue is directly fixed in
pytest, if it is one day.It is also compliant with
@cases_dataas shown here.Note: it works by creating sub-fixtures under the hood, just like the ones you showed in your workaround. So thanks for the idea! :)
smarie commentedon Feb 1, 2019
Update: the execution order was a bit random, I fixed it in 1.2.0, see smarie/python-pytest-cases#22 .
Sup3rGeo commentedon Mar 16, 2019
I would love to see this implemented, as I see no reason why we cannot use the same way to parameterize fixtures and test functions.
@smarie does your improved fixture accept also parameterizing pair of arguments? like:
smarie commentedon Mar 18, 2019
yes it does support multi-parameters. Let me know (please post an issue) if it does not work for you.
Note: the decoration order matters,
@pytest_fixture_plusshould be on top so that it is able to "see" the marks. So the correct way is:Sup3rGeo commentedon Mar 18, 2019
I created an issue in: smarie/python-pytest-cases#26
RonnyPfannschmidt commentedon Mar 18, 2019
This directly ties into a critical topic - how should marks pass from fixture to test,
there is no simple solution
smarie commentedon Mar 19, 2019
@Sup3rGeo this was a regression, the issue is now fixed in
pytest-cases1.3.2.@RonnyPfannschmidt my two cents on this topic for what's worth: if the problem is passing marks, then it might be worth considering to make the parametrization decorator a first-class citizen and not a mark anymore. And/or to provide a parametrization decorator that "feels the same" for users, but is implemented differently whether you parametrize test functions or fixtures. Implementation can then rely on marks if needed, but that's an implementation decision, not user's business.
Sup3rGeo commentedon Mar 19, 2019
@smarie I would agree 100%, and not only for parametrization, but perhaps also the other concepts that are treated/implemented as marks and should be first-class citizens (skip, skipif, xfail, etc). I discussed a little bit about it in #4081
RonnyPfannschmidt commentedon Mar 19, 2019
@smarie another issue name-spacing the new parameters - parameters are fixtures with direct or indirect values, so they naturally spill to the test in some way
smarie commentedon Mar 19, 2019
If I understand your comment correctly, you are saying that what is proposed in pytest-cases and above is not an ideal longterm solution. I fully agree, it is indeed a workaround.
I try to make the generated fixture names explicit and unique (see source):
gen_name = fixture_func.__name__ + "__" + 'X'.join(m.param_names)But they are still generated fixtures, so that's not entirely transparent. At least it allows us to work while this makes its way in the core
pytestdesign (I understand that some major refactoring would be needed, so there is some interest in having something usable in the meantime).5 remaining items
smarie commentedon Mar 28, 2019
No worries, I understand that you have put a lot of efforts to bring
pytestto the state it is today and that rest is well deserved before a major revisit of the internal architecture - if it happens.In the meantime that's absolutely not a problem, since
pytest-caseshas been validated against several versions of pytest and is in a stable production state. We can certainly live with this workaround for months if not more :)Conchylicultor commentedon Sep 21, 2020
Any updates on this ? Mixing the
@pytest.mark.parametrizewith@pytest.fixturewould feel very natural and I was surprise pytest did not support this already.tanev commentedon Sep 22, 2020
@Conchylicultor, just mentioning something that works very nicely, but might not be what you need:
...might be of help in such cases, it has its own set of limitations, though.
smarie commentedon Sep 22, 2020
@Conchylicultor there is a workaround until this is baked in
pytest:pip install pytest-casesand then :Conchylicultor commentedon Sep 22, 2020
@smarie Yes, I'm aware of
pytest-case, but I would prefer not having to install extra modules for something as simple. Also,pytest-caseisn't available at Google currently.@tanev, this feature request is about parametrize the fixture, not the test. You solution only works mostly when a single test is parametrized. Also the indirection makes it too "magic".
RonnyPfannschmidt commentedon Sep 22, 2020
In near future its impossible to do in core
What looks simply has a massive amount of edge cases we currently can't sanely manage, so for now, marks won't work for fixtures
smarie commentedon Sep 22, 2020
Looks like making
pytest-casesavailable at Google would be easier than refactoringpytestinternals ;) Thanks for the precision @RonnyPfannschmidt . I confirm that the only way I found to do this in pytest-cases is to create a single param with the correct cross-product of values, ids and marks. See here in the code of@fixturenicoddemus commentedon Sep 23, 2020
Just to add to @RonnyPfannschmidt's response: the main issue about adding this support is not parametrization per-se, but adding mark support for fixtures, which needs some careful consideration of the repercussions of that.
The "core" implementation of parametrization in pytest is
pytest_generate_tests, which is a generic way to implement parametrization, for any test, based on anything you can imagine.This is how the pytest core implements parametrization support out of the box for general use:
For tests, a
pytest_generate_testsimplementation will check if the test is marked withpytest.mark.parametrizeand generate extra tests accordingly.For fixtures, another
pytest_generate_testsimplementation will generate new tests based on the fixtures used by the function.Sup3rGeo commentedon Sep 23, 2020
It seems to me that it might be that the
markthing is bringing more complexity than it is needed.The parametrization of fixtures from @smarie seems to be already what me and other uses typically need and expect from such feature - at the same time, I don't see how it is or should be related to the problem related to passing marks from fixtures to tests.
Generally speaking, I see a completely different usage for general
@pytest.markand parametrization (either test functions or fixtures). I tend to think that using marks for that matter might not be the right solution and parametrization must be a "first class citizen" (even maybe@pytest.parametrize, without marks) instead of being some side-benefit of marks, so we effectively decouple the two problems.nicoddemus commentedon Sep 23, 2020
You are correct in that it could be treated as separate concerns, I just wanted to address explicitly the title of the issue ("why isn't @pytest.mark.parametrize work on fixures already?").
To elaborate:
@pytest.fixtureis just a decorator that essentially appends metadata to the decorated function, which is picked up during collection.A new
@pytest.parametrizedecorator could do the same thing: append metadata to the function it decorates, be it either a fixture or a test function, which is then processed by a custompytest_generate_testshook.I'm pretty sure the idea above can be implemented outside the core even, if someone wants to play with it.
Having said that, I'm not a big fan of the idea in principle: creating another way to do the same thing is confusing for users and a maintenance problem for the project (both in code to maintain and the possibility of bugs that will arise from that implementation).
Conceptually, I think supporting marks in fixtures makes sense (and in turn make fixture parametrization that much nicer), but we need to think carefully what it means.
For parametrize, the answer is "obvious":
But not so much in the general sense:
What happens with "X"? Are marks applied to fixtures are automatically propagated to tests which use it? How about the mark arguments,
{"hello": "world"}, how do I access them?Internally, there's another problem, marks are applied to Nodes (test functions and classes), and fixtures are not part of the collection tree.
All those details need to figure out first.
Sup3rGeo commentedon Sep 23, 2020
Thanks for the detailed answer @nicoddemus
Fair point!
If I understood correctly from your words:
@pytest.fixture, with just some metadata forpytest_generate_tests), but with potential confusion for users/maintainability@pytest.mark.fixture), but have to deal with the bigger, unrelated, more complicated mark propagation problemPersonally I don't think it is even worth thinking about the second point - As in the end there is the user not having a very helpful feature because of some mark behavior he probably would not care at all (e.g. I never had the need for marking a fixture myself).
Although of course it could be that just the implementation of the first point could prove too complicated itself but I guess it is always the case with new features.
The-Compiler commentedon Sep 24, 2020
@Sup3rGeo I'm confused about you referring to
@pytest.fixtureand@pytest.mark.fixture- did you mean@pytest.parametrizeand@pytest.mark.parametrizethere?smarie commentedon Oct 9, 2020
Not directly related, but connected: I have created a plugin to design higher-level marks for filtering. It is named pytest-pilot. This is maybe another example of the huge gap that @Sup3rGeo pointed out, between parametrization marks (that are closely related to the core pytest engine itself) and other marks (that, as demonstrated by this extension, are quite "far" from the core of pytest and can be manipulated easily without hacking into the pytest engine. No new debate here, just food for thoughts :) .
Zac-HD commentedon Jul 21, 2026
Closing as won't-fix per maintainer consensus: marks on fixtures won't be supported — propagating marks/parametrization from fixtures has unmanageable edge cases per the discussion above, and applying a mark to a fixture is now a hard error (deprecated via #3664, error since pytest 9.0). The
pytest-casesplugin implements this if you need it.