I'm writing some unit tests using Pytest and came across two ways to parameterize test inputs. One is using parametereized fixtures and the other is using the pytest.mark.parametrize
method.
The two examples I have are:
# method 1
def tokens():
yield from ["+", "*", "?"]
@pytest.mark.parametrize("token", tokens())
def test_stuff(token):
assert stuff
and
# method 2
@pytest.fixture(params=["+", "*", "?"])
def token(request):
return request.param
def test_stuff(token):
assert stuff
Both have different advantages and disadvantages from what I can tell:
Method 1
Advantages
- Supports multiple parameters
- Supports lazy evaluation
Disadvantages
- More boilerplate code when used for multiple test methods
- requires explicit parameter mapping for every method used, even if parameters are the same name
method 2
Advantages
- Less Boiler Plate code
Disadvantages
- Only allows single parameter to be passed to unit test
I'm still new to PyTest so maybe there is a way around the disadvantages that I listed above for each method but given those I have been having a hard time trying to decide which one to use. I would guess that the intended way to do what I am trying to do is to use @pytest.mark.parametrize
but when passing only a single parameter having less boilerplate code by using a fixture seems like a big advantage. Can anyone tell me a reason not to do it this way or is this a perfectly valid use case?