pytest enables test parametrization at several levels:
@pytest.mark.parametrize: parametrizing test functions
The builtin pytest.mark.parametrize decorator enables parametrization of arguments for a test function. Here is a typical example of a test function that implements checking that a certain input leads to an expected output:
As designed in this example, only one pair of input/output values fails the simple test function. And as usual with test function arguments, you can see the input and output values in the traceback. Note that you could also use the parametrize marker on a class or a module (see Marking test functions with attributes) which would invoke several functions with the argument sets.
It is also possible to mark individual test instances within parametrize, for example with the builtin mark.xfail:
The one parameter set which caused a failure previously now shows up as an “xfailed (expected to fail)” test. To get all combinations of multiple parametrized arguments you can stack parametrize decorators:
Basic pytest_generate_tests example
Sometimes you may want to implement your own parametrization scheme or implement some dynamism for determining the parameters or scope of a fixture. For this, you can use the pytest_generate_tests hook which is called when collecting a test function. Through the passed in metafunc object you can inspect the requesting test context and, most importantly, you can call metafunc.parametrize() to cause parametrization.
Let’s also run with a stringinput that will lead to a failing test:
As expected our test function fails. If you don’t specify a stringinput it will be skipped because metafunc.parametrize() will be called with an empty parameter list:
Note that when calling metafunc.parametrize multiple times with different parameter sets, all parameter names across those sets cannot be duplicated, otherwise an error will be raised. For further examples, you might want to look at more parametrization examples.