I commonly find myself writing fixtures which are basically listings of some parameters:
@pytest.fixture(scope='module', params=[(64, 64), (64, 128), (128, 128)])
def shape(request):
return request.param
I suggest that a function should be added that simplfies this. One example would be
shape = pytest.simple_fixture(params=[(64, 64), (64, 128), (128, 128)])
This would significantly reduce repetition in code, and is also much more readable in my opinion.
If you would be interested, I could make a PR with this.
This sounds like you'd simply want to use @pytest.mark.parametrize, no?
Well it is not strictly equivalent. Specifically pytest.mark.parametrize is not re-usable (in a trivial way) the same way fixtures are.
Basically what I suggest is:
shape = pytest.simple_fixture(params=[(64, 64), (128, 128)])
def test_something(shape):
assert True
def test_something_else(shape):
assert True
vs the current:
@pytest.mark.parametrize("shape", [(64, 64), (128, 128)])
def test_something(shape):
assert True
@pytest.mark.parametrize("shape", [(64, 64), (128, 128)])
def test_something_else(shape):
assert True
or without as much repetition:
parametrizer = pytest.mark.parametrize("shape", [(64, 64), (128, 128)])
@parametrizer
def test_something(shape):
assert True
@parametrizer
def test_something_else(shape):
assert True
I guess the improvement is somewhat minor, but it does save some lines, especially when you start needing several fixtures, or once you start writing fixtures that depend on fixtures.
IMHO the last form is pretty good, all you need is a simple decorator applied to each test; I'm not sure it is worth adding a new function because of it.
Also I'm pretty sure you will need to pass in the name the fixture to make it work:
shape = pytest.simple_fixture("shape", params=[(64, 64), (128, 128)])
If in the end we decide to not add this to the core, I would like to mention that the implementation is pretty simple and easy to make into a third-party plugin if the OP wants to:
def pytest_configure():
import pytest
pytest.simple_fixture = simple_fixture
def simple_fixture(name, params):
@pytest.fixture(name=name, params=params)
def inner(request):
return request.param
return inner
FWIW I'm not really keen about having yet another way to do parametrization - from my experience giving pytest trainings and such, the difference between parametrizing tests and parametrizing fixtures is already confusing enough for newcomers.
This is closely related to what I looking for in #2855, so I decided to support simple fixtures as well in pytest-parametrized.
shape = pytest.parametrized.fixture((64, 64), (128, 128))
Closing this issue because there are adequate workarounds and an external implementation, so we don't need to make Pytest itself more complicated.
Most helpful comment
FWIW I'm not really keen about having yet another way to do parametrization - from my experience giving pytest trainings and such, the difference between parametrizing tests and parametrizing fixtures is already confusing enough for newcomers.