Preface
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:
- test.py
- # content of test_expectation.py
- import pytest
- @pytest.mark.parametrize("test_input,expected", [
- ("3+5", 8),
- ("2+4", 6),
- ("6*9", 42),
- ])
- def test_eval(test_input, expected):
- assert eval(test_input) == expected
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:
- test2.py
- # content of test_expectation.py
- import pytest
- @pytest.mark.parametrize("test_input,expected", [
- ("3+5", 8),
- ("2+4", 6),
- pytest.param("6*9", 42,
- marks=pytest.mark.xfail),
- ])
- def test_eval(test_input, expected):
- assert eval(test_input) == expected
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:
- import pytest
- @pytest.mark.parametrize("x", [0, 1])
- @pytest.mark.parametrize("y", [2, 3])
- def test_foo(x, y):
- pass
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.
- test_strings.py
- # content of test_strings.py
- def test_valid_string(stringinput):
- assert stringinput.isalpha()
- conftest.py
- # content of conftest.py
- def pytest_addoption(parser):
- parser.addoption("--stringinput", action="append", default=[],
- help="list of stringinputs to pass to test functions")
- def pytest_generate_tests(metafunc):
- if 'stringinput' in metafunc.fixturenames:
- metafunc.parametrize("stringinput",
- metafunc.config.getoption('stringinput'))
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.
沒有留言:
張貼留言