test_ok2/doc/en/how-to/parametrize.rst

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

299 lines
9.6 KiB
ReStructuredText
Raw Normal View History

.. _`test generators`:
.. _`parametrizing-tests`:
.. _`parametrized test functions`:
.. _`parametrize`:
2012-10-18 18:24:50 +08:00
.. _`parametrize-basics`:
How to parametrize fixtures and test functions
==========================================================================
pytest enables test parametrization at several levels:
- :py:func:`pytest.fixture` allows one to :ref:`parametrize fixture
functions <fixture-parametrize>`.
* `@pytest.mark.parametrize`_ allows one to define multiple sets of
arguments and fixtures at the test function or class.
* `pytest_generate_tests`_ allows one to define custom parametrization
schemes or extensions.
2012-10-18 18:24:50 +08:00
.. _parametrizemark:
.. _`@pytest.mark.parametrize`:
``@pytest.mark.parametrize``: parametrizing test functions
---------------------------------------------------------------------
.. regendoc: wipe
Several improvements.
2018-03-01 07:34:20 +08:00
The builtin :ref:`pytest.mark.parametrize ref` 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
2019-04-12 19:50:26 +08:00
to an expected output:
.. code-block:: python
# content of test_expectation.py
import pytest
2019-04-12 19:50:26 +08:00
@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
Here, the ``@parametrize`` decorator defines three different ``(test_input,expected)``
tuples so that the ``test_eval`` function will run three times using
2018-11-24 13:41:22 +08:00
them in turn:
.. code-block:: pytest
$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
collected 3 items
test_expectation.py ..F [100%]
================================= FAILURES =================================
____________________________ test_eval[6*9-42] _____________________________
test_input = '6*9', expected = 42
2019-04-15 22:24:17 +08:00
@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
E AssertionError: assert 54 == 42
E + where 54 = eval('6*9')
2019-04-15 22:24:17 +08:00
test_expectation.py:6: AssertionError
========================= short test summary info ==========================
FAILED test_expectation.py::test_eval[6*9-42] - AssertionError: assert 54...
2019-08-30 23:43:47 +08:00
======================= 1 failed, 2 passed in 0.12s ========================
.. note::
Parameter values are passed as-is to tests (no copy whatsoever).
For example, if you pass a list or a dict as a parameter value, and
the test case code mutates it, the mutations will be reflected in subsequent
test case calls.
.. note::
pytest by default escapes any non-ascii characters used in unicode strings
for the parametrization because it has several downsides.
If however you would like to use unicode strings in parametrization
and see them in the terminal as is (non-escaped), use this option
in your ``pytest.ini``:
.. code-block:: ini
[pytest]
disable_test_id_escaping_and_forfeit_all_rights_to_community_support = True
2019-03-27 23:05:33 +08:00
Keep in mind however that this might cause unwanted side effects and
even bugs depending on the OS used and plugins currently installed,
so use it at your own risk.
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 :ref:`mark`) which would invoke several functions with the argument sets,
for instance:
.. code-block:: python
import pytest
@pytest.mark.parametrize("n,expected", [(1, 2), (3, 4)])
class TestClass:
def test_simple_case(self, n, expected):
assert n + 1 == expected
def test_weird_simple_case(self, n, expected):
assert (n * 1) + 1 == expected
To parametrize all tests in a module, you can assign to the :globalvar:`pytestmark` global variable:
.. code-block:: python
import pytest
pytestmark = pytest.mark.parametrize("n,expected", [(1, 2), (3, 4)])
class TestClass:
def test_simple_case(self, n, expected):
assert n + 1 == expected
def test_weird_simple_case(self, n, expected):
assert (n * 1) + 1 == expected
It is also possible to mark individual test instances within parametrize,
2019-04-12 19:50:26 +08:00
for example with the builtin ``mark.xfail``:
.. code-block:: python
2013-05-21 09:12:45 +08:00
# content of test_expectation.py
import pytest
2019-04-12 19:50:26 +08:00
@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
2013-05-21 09:12:45 +08:00
2018-11-24 13:41:22 +08:00
Let's run this:
.. code-block:: pytest
$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
collected 3 items
test_expectation.py ..x [100%]
2019-08-30 23:43:47 +08:00
======================= 2 passed, 1 xfailed in 0.12s =======================
The one parameter set which caused a failure previously now
2020-06-11 16:22:47 +08:00
shows up as an "xfailed" (expected to fail) test.
In case the values provided to ``parametrize`` result in an empty list - for
example, if they're dynamically generated by some function - the behaviour of
pytest is defined by the :confval:`empty_parameter_set_mark` option.
To get all combinations of multiple parametrized arguments you can stack
2019-04-12 19:50:26 +08:00
``parametrize`` decorators:
.. code-block:: python
import pytest
2019-04-12 19:50:26 +08:00
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
pass
This will run the test with the arguments set to ``x=0/y=2``, ``x=1/y=2``,
2017-12-16 23:31:48 +08:00
``x=0/y=3``, and ``x=1/y=3`` exhausting parameters in the order of the decorators.
.. _`pytest_generate_tests`:
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
2016-02-16 06:19:07 +08:00
``metafunc`` object you can inspect the requesting test context and, most
importantly, you can call ``metafunc.parametrize()`` to cause
parametrization.
For example, let's say we want to run a test taking string inputs which
we want to set via a new ``pytest`` command line option. Let's first write
2019-04-12 19:50:26 +08:00
a simple test accepting a ``stringinput`` fixture function argument:
.. code-block:: python
# content of test_strings.py
2019-04-12 19:50:26 +08:00
def test_valid_string(stringinput):
assert stringinput.isalpha()
Now we add a ``conftest.py`` file containing the addition of a
2019-04-12 19:50:26 +08:00
command line option and the parametrization of our test function:
.. code-block:: python
# content of conftest.py
2019-04-12 19:50:26 +08:00
def pytest_addoption(parser):
2019-04-12 19:50:26 +08:00
parser.addoption(
"--stringinput",
action="append",
default=[],
help="list of stringinputs to pass to test functions",
)
def pytest_generate_tests(metafunc):
2019-04-12 19:50:26 +08:00
if "stringinput" in metafunc.fixturenames:
metafunc.parametrize("stringinput", metafunc.config.getoption("stringinput"))
If we now pass two stringinput values, our test will run twice:
.. code-block:: pytest
$ pytest -q --stringinput="hello" --stringinput="world" test_strings.py
.. [100%]
2019-09-18 21:11:59 +08:00
2 passed in 0.12s
2018-11-24 13:41:22 +08:00
Let's also run with a stringinput that will lead to a failing test:
.. code-block:: pytest
$ pytest -q --stringinput="!" test_strings.py
F [100%]
================================= FAILURES =================================
___________________________ test_valid_string[!] ___________________________
stringinput = '!'
def test_valid_string(stringinput):
> assert stringinput.isalpha()
E AssertionError: assert False
E + where False = <built-in method isalpha of str object at 0xdeadbeef0001>()
E + where <built-in method isalpha of str object at 0xdeadbeef0001> = '!'.isalpha
2019-04-15 22:24:17 +08:00
test_strings.py:4: AssertionError
========================= short test summary info ==========================
FAILED test_strings.py::test_valid_string[!] - AssertionError: assert False
2019-09-18 21:11:59 +08:00
1 failed in 0.12s
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
2018-11-24 13:41:22 +08:00
list:
.. code-block:: pytest
$ pytest -q -rs test_strings.py
s [100%]
========================= short test summary info ==========================
SKIPPED [1] test_strings.py: got empty parameter set ['stringinput'], function test_valid_string at /home/sweet/project/test_strings.py:2
2019-09-18 21:11:59 +08:00
1 skipped in 0.12s
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.
More examples
-------------
For further examples, you might want to look at :ref:`more
parametrization examples <paramexamples>`.