| 1 |
|
| 2 |
.. _`test generators`: |
| 3 |
.. _`parametrizing-tests`: |
| 4 |
.. _`parametrized test functions`: |
| 5 |
.. _`parametrize`: |
| 6 |
|
| 7 |
.. _`parametrize-basics`: |
| 8 |
|
| 9 |
How to parametrize fixtures and test functions |
| 10 |
========================================================================== |
| 11 |
|
| 12 |
pytest enables test parametrization at several levels: |
| 13 |
|
| 14 |
- :py:func:`pytest.fixture` allows one to :ref:`parametrize fixture |
| 15 |
functions <fixture-parametrize>`. |
| 16 |
|
| 17 |
* `@pytest.mark.parametrize`_ allows one to define multiple sets of |
| 18 |
arguments and fixtures at the test function or class. |
| 19 |
|
| 20 |
* `pytest_generate_tests`_ allows one to define custom parametrization |
| 21 |
schemes or extensions. |
| 22 |
|
| 23 |
.. _parametrizemark: |
| 24 |
.. _`@pytest.mark.parametrize`: |
| 25 |
|
| 26 |
|
| 27 |
``@pytest.mark.parametrize``: parametrizing test functions |
| 28 |
--------------------------------------------------------------------- |
| 29 |
|
| 30 |
.. regendoc: wipe |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
Several improvements. |
| 35 |
|
| 36 |
The builtin :ref:`pytest.mark.parametrize ref` decorator enables |
| 37 |
parametrization of arguments for a test function. Here is a typical example |
| 38 |
of a test function that implements checking that a certain input leads |
| 39 |
to an expected output: |
| 40 |
|
| 41 |
.. code-block:: python |
| 42 |
|
| 43 |
# content of test_expectation.py |
| 44 |
import pytest |
| 45 |
|
| 46 |
|
| 47 |
@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)]) |
| 48 |
def test_eval(test_input, expected): |
| 49 |
assert eval(test_input) == expected |
| 50 |
|
| 51 |
Here, the ``@parametrize`` decorator defines three different ``(test_input,expected)`` |
| 52 |
tuples so that the ``test_eval`` function will run three times using |
| 53 |
them in turn: |
| 54 |
|
| 55 |
.. code-block:: pytest |
| 56 |
|
| 57 |
$ pytest |
| 58 |
=========================== test session starts ============================ |
| 59 |
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y |
| 60 |
rootdir: /home/sweet/project |
| 61 |
collected 3 items |
| 62 |
|
| 63 |
test_expectation.py ..F [100%] |
| 64 |
|
| 65 |
================================= FAILURES ================================= |
| 66 |
____________________________ test_eval[6*9-42] _____________________________ |
| 67 |
|
| 68 |
test_input = '6*9', expected = 42 |
| 69 |
|
| 70 |
@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)]) |
| 71 |
def test_eval(test_input, expected): |
| 72 |
> assert eval(test_input) == expected |
| 73 |
E AssertionError: assert 54 == 42 |
| 74 |
E + where 54 = eval('6*9') |
| 75 |
|
| 76 |
test_expectation.py:6: AssertionError |
| 77 |
========================= short test summary info ========================== |
| 78 |
FAILED test_expectation.py::test_eval[6*9-42] - AssertionError: assert 54... |
| 79 |
======================= 1 failed, 2 passed in 0.12s ======================== |
| 80 |
|
| 81 |
.. note:: |
| 82 |
|
| 83 |
Parameter values are passed as-is to tests (no copy whatsoever). |
| 84 |
|
| 85 |
For example, if you pass a list or a dict as a parameter value, and |
| 86 |
the test case code mutates it, the mutations will be reflected in subsequent |
| 87 |
test case calls. |
| 88 |
|
| 89 |
.. note:: |
| 90 |
|
| 91 |
pytest by default escapes any non-ascii characters used in unicode strings |
| 92 |
for the parametrization because it has several downsides. |
| 93 |
If however you would like to use unicode strings in parametrization |
| 94 |
and see them in the terminal as is (non-escaped), use this option |
| 95 |
in your ``pytest.ini``: |
| 96 |
|
| 97 |
.. code-block:: ini |
| 98 |
|
| 99 |
[pytest] |
| 100 |
disable_test_id_escaping_and_forfeit_all_rights_to_community_support = True |
| 101 |
|
| 102 |
Keep in mind however that this might cause unwanted side effects and |
| 103 |
even bugs depending on the OS used and plugins currently installed, |
| 104 |
so use it at your own risk. |
| 105 |
|
| 106 |
|
| 107 |
As designed in this example, only one pair of input/output values fails |
| 108 |
the simple test function. And as usual with test function arguments, |
| 109 |
you can see the ``input`` and ``output`` values in the traceback. |
| 110 |
|
| 111 |
Note that you could also use the parametrize marker on a class or a module |
| 112 |
(see :ref:`mark`) which would invoke several functions with the argument sets, |
| 113 |
for instance: |
| 114 |
|
| 115 |
|
| 116 |
.. code-block:: python |
| 117 |
|
| 118 |
import pytest |
| 119 |
|
| 120 |
|
| 121 |
@pytest.mark.parametrize("n,expected", [(1, 2), (3, 4)]) |
| 122 |
class TestClass: |
| 123 |
def test_simple_case(self, n, expected): |
| 124 |
assert n + 1 == expected |
| 125 |
|
| 126 |
def test_weird_simple_case(self, n, expected): |
| 127 |
assert (n * 1) + 1 == expected |
| 128 |
|
| 129 |
|
| 130 |
To parametrize all tests in a module, you can assign to the :globalvar:`pytestmark` global variable: |
| 131 |
|
| 132 |
|
| 133 |
.. code-block:: python |
| 134 |
|
| 135 |
import pytest |
| 136 |
|
| 137 |
pytestmark = pytest.mark.parametrize("n,expected", [(1, 2), (3, 4)]) |
| 138 |
|
| 139 |
|
| 140 |
class TestClass: |
| 141 |
def test_simple_case(self, n, expected): |
| 142 |
assert n + 1 == expected |
| 143 |
|
| 144 |
def test_weird_simple_case(self, n, expected): |
| 145 |
assert (n * 1) + 1 == expected |
| 146 |
|
| 147 |
|
| 148 |
It is also possible to mark individual test instances within parametrize, |
| 149 |
for example with the builtin ``mark.xfail``: |
| 150 |
|
| 151 |
.. code-block:: python |
| 152 |
|
| 153 |
# content of test_expectation.py |
| 154 |
import pytest |
| 155 |
|
| 156 |
|
| 157 |
@pytest.mark.parametrize( |
| 158 |
"test_input,expected", |
| 159 |
[("3+5", 8), ("2+4", 6), pytest.param("6*9", 42, marks=pytest.mark.xfail)], |
| 160 |
) |
| 161 |
def test_eval(test_input, expected): |
| 162 |
assert eval(test_input) == expected |
| 163 |
|
| 164 |
Let's run this: |
| 165 |
|
| 166 |
.. code-block:: pytest |
| 167 |
|
| 168 |
$ pytest |
| 169 |
=========================== test session starts ============================ |
| 170 |
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y |
| 171 |
rootdir: /home/sweet/project |
| 172 |
collected 3 items |
| 173 |
|
| 174 |
test_expectation.py ..x [100%] |
| 175 |
|
| 176 |
======================= 2 passed, 1 xfailed in 0.12s ======================= |
| 177 |
|
| 178 |
The one parameter set which caused a failure previously now |
| 179 |
shows up as an "xfailed" (expected to fail) test. |
| 180 |
|
| 181 |
In case the values provided to ``parametrize`` result in an empty list - for |
| 182 |
example, if they're dynamically generated by some function - the behaviour of |
| 183 |
pytest is defined by the :confval:`empty_parameter_set_mark` option. |
| 184 |
|
| 185 |
To get all combinations of multiple parametrized arguments you can stack |
| 186 |
``parametrize`` decorators: |
| 187 |
|
| 188 |
.. code-block:: python |
| 189 |
|
| 190 |
import pytest |
| 191 |
|
| 192 |
|
| 193 |
@pytest.mark.parametrize("x", [0, 1]) |
| 194 |
@pytest.mark.parametrize("y", [2, 3]) |
| 195 |
def test_foo(x, y): |
| 196 |
pass |
| 197 |
|
| 198 |
This will run the test with the arguments set to ``x=0/y=2``, ``x=1/y=2``, |
| 199 |
``x=0/y=3``, and ``x=1/y=3`` exhausting parameters in the order of the decorators. |
| 200 |
|
| 201 |
.. _`pytest_generate_tests`: |
| 202 |
|
| 203 |
Basic ``pytest_generate_tests`` example |
| 204 |
--------------------------------------------- |
| 205 |
|
| 206 |
Sometimes you may want to implement your own parametrization scheme |
| 207 |
or implement some dynamism for determining the parameters or scope |
| 208 |
of a fixture. For this, you can use the ``pytest_generate_tests`` hook |
| 209 |
which is called when collecting a test function. Through the passed in |
| 210 |
``metafunc`` object you can inspect the requesting test context and, most |
| 211 |
importantly, you can call ``metafunc.parametrize()`` to cause |
| 212 |
parametrization. |
| 213 |
|
| 214 |
For example, let's say we want to run a test taking string inputs which |
| 215 |
we want to set via a new ``pytest`` command line option. Let's first write |
| 216 |
a simple test accepting a ``stringinput`` fixture function argument: |
| 217 |
|
| 218 |
.. code-block:: python |
| 219 |
|
| 220 |
# content of test_strings.py |
| 221 |
|
| 222 |
|
| 223 |
def test_valid_string(stringinput): |
| 224 |
assert stringinput.isalpha() |
| 225 |
|
| 226 |
Now we add a ``conftest.py`` file containing the addition of a |
| 227 |
command line option and the parametrization of our test function: |
| 228 |
|
| 229 |
.. code-block:: python |
| 230 |
|
| 231 |
# content of conftest.py |
| 232 |
|
| 233 |
|
| 234 |
def pytest_addoption(parser): |
| 235 |
parser.addoption( |
| 236 |
"--stringinput", |
| 237 |
action="append", |
| 238 |
default=[], |
| 239 |
help="list of stringinputs to pass to test functions", |
| 240 |
) |
| 241 |
|
| 242 |
|
| 243 |
def pytest_generate_tests(metafunc): |
| 244 |
if "stringinput" in metafunc.fixturenames: |
| 245 |
metafunc.parametrize("stringinput", metafunc.config.getoption("stringinput")) |
| 246 |
|
| 247 |
If we now pass two stringinput values, our test will run twice: |
| 248 |
|
| 249 |
.. code-block:: pytest |
| 250 |
|
| 251 |
$ pytest -q --stringinput="hello" --stringinput="world" test_strings.py |
| 252 |
.. [100%] |
| 253 |
2 passed in 0.12s |
| 254 |
|
| 255 |
Let's also run with a stringinput that will lead to a failing test: |
| 256 |
|
| 257 |
.. code-block:: pytest |
| 258 |
|
| 259 |
$ pytest -q --stringinput="!" test_strings.py |
| 260 |
F [100%] |
| 261 |
================================= FAILURES ================================= |
| 262 |
___________________________ test_valid_string[!] ___________________________ |
| 263 |
|
| 264 |
stringinput = '!' |
| 265 |
|
| 266 |
def test_valid_string(stringinput): |
| 267 |
> assert stringinput.isalpha() |
| 268 |
E AssertionError: assert False |
| 269 |
E + where False = <built-in method isalpha of str object at 0xdeadbeef0001>() |
| 270 |
E + where <built-in method isalpha of str object at 0xdeadbeef0001> = '!'.isalpha |
| 271 |
|
| 272 |
test_strings.py:4: AssertionError |
| 273 |
========================= short test summary info ========================== |
| 274 |
FAILED test_strings.py::test_valid_string[!] - AssertionError: assert False |
| 275 |
1 failed in 0.12s |
| 276 |
|
| 277 |
As expected our test function fails. |
| 278 |
|
| 279 |
If you don't specify a stringinput it will be skipped because |
| 280 |
``metafunc.parametrize()`` will be called with an empty parameter |
| 281 |
list: |
| 282 |
|
| 283 |
.. code-block:: pytest |
| 284 |
|
| 285 |
$ pytest -q -rs test_strings.py |
| 286 |
s [100%] |
| 287 |
========================= short test summary info ========================== |
| 288 |
SKIPPED [1] test_strings.py: got empty parameter set ['stringinput'], function test_valid_string at /home/sweet/project/test_strings.py:2 |
| 289 |
1 skipped in 0.12s |
| 290 |
|
| 291 |
Note that when calling ``metafunc.parametrize`` multiple times with different parameter sets, all parameter names across |
| 292 |
those sets cannot be duplicated, otherwise an error will be raised. |
| 293 |
|
| 294 |
More examples |
| 295 |
------------- |
| 296 |
|
| 297 |
For further examples, you might want to look at :ref:`more |
| 298 |
parametrization examples <paramexamples>`. |