2012-10-07 19:06:17 +08:00
|
|
|
|
|
|
|
.. _`test generators`:
|
|
|
|
.. _`parametrizing-tests`:
|
|
|
|
.. _`parametrized test functions`:
|
|
|
|
.. _`parametrize`:
|
|
|
|
|
2012-10-18 18:24:50 +08:00
|
|
|
.. _`parametrize-basics`:
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
Parametrizing fixtures and test functions
|
|
|
|
==========================================================================
|
|
|
|
|
2012-10-08 19:19:31 +08:00
|
|
|
pytest supports test parametrization in several well-integrated ways:
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
- :py:func:`pytest.fixture` allows to define :ref:`parametrization
|
2012-10-08 19:19:31 +08:00
|
|
|
at the level of fixture functions <fixture-parametrize>`.
|
|
|
|
|
|
|
|
* `@pytest.mark.parametrize`_ allows to define parametrization at the
|
2014-01-18 19:31:33 +08:00
|
|
|
function or class level, provides multiple argument/fixture sets
|
2012-10-07 19:06:17 +08:00
|
|
|
for a particular test function or class.
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
* `pytest_generate_tests`_ enables implementing your own custom
|
2012-10-08 19:19:31 +08:00
|
|
|
dynamic parametrization scheme or extensions.
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2012-10-18 18:24:50 +08:00
|
|
|
.. _parametrizemark:
|
2012-10-07 19:06:17 +08:00
|
|
|
.. _`@pytest.mark.parametrize`:
|
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
``@pytest.mark.parametrize``: parametrizing test functions
|
|
|
|
---------------------------------------------------------------------
|
|
|
|
|
|
|
|
.. regendoc: wipe
|
|
|
|
|
2013-05-28 16:32:54 +08:00
|
|
|
.. versionadded:: 2.2, improved in 2.4
|
2012-10-07 19:06:17 +08:00
|
|
|
|
|
|
|
The builtin ``pytest.mark.parametrize`` decorator enables
|
|
|
|
parametrization of arguments for a test function. Here is a typical example
|
2012-10-08 19:19:31 +08:00
|
|
|
of a test function that implements checking that a certain input leads
|
|
|
|
to an expected output::
|
2012-10-07 19:06:17 +08:00
|
|
|
|
|
|
|
# content of test_expectation.py
|
|
|
|
import pytest
|
2013-05-28 16:32:54 +08:00
|
|
|
@pytest.mark.parametrize("input,expected", [
|
2012-10-07 19:06:17 +08:00
|
|
|
("3+5", 8),
|
|
|
|
("2+4", 6),
|
|
|
|
("6*9", 42),
|
|
|
|
])
|
|
|
|
def test_eval(input, expected):
|
|
|
|
assert eval(input) == expected
|
|
|
|
|
2014-01-10 05:27:23 +08:00
|
|
|
Here, the ``@parametrize`` decorator defines three different ``(input,expected)``
|
|
|
|
tuples so that the ``test_eval`` function will run three times using
|
2013-05-28 16:32:54 +08:00
|
|
|
them in turn::
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
$ py.test
|
2015-06-07 05:30:49 +08:00
|
|
|
======= test session starts ========
|
|
|
|
platform linux2 -- Python 2.7.9, pytest-2.8.0.dev4, py-1.4.28, pluggy-0.3.0
|
|
|
|
rootdir: $REGENDOC_TMPDIR, inifile:
|
2012-10-09 20:35:17 +08:00
|
|
|
collected 3 items
|
2014-01-29 20:47:11 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
test_expectation.py ..F
|
2014-01-29 20:47:11 +08:00
|
|
|
|
2015-06-07 05:30:49 +08:00
|
|
|
======= FAILURES ========
|
|
|
|
_______ test_eval[6*9-42] ________
|
2014-01-29 20:47:11 +08:00
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
input = '6*9', expected = 42
|
2014-01-29 20:47:11 +08:00
|
|
|
|
2013-05-28 16:32:54 +08:00
|
|
|
@pytest.mark.parametrize("input,expected", [
|
2012-10-07 19:06:17 +08:00
|
|
|
("3+5", 8),
|
|
|
|
("2+4", 6),
|
|
|
|
("6*9", 42),
|
|
|
|
])
|
|
|
|
def test_eval(input, expected):
|
|
|
|
> assert eval(input) == expected
|
|
|
|
E assert 54 == 42
|
|
|
|
E + where 54 = eval('6*9')
|
2014-01-29 20:47:11 +08:00
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
test_expectation.py:8: AssertionError
|
2015-06-07 05:30:49 +08:00
|
|
|
======= 1 failed, 2 passed in 0.12 seconds ========
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2013-05-28 16:32:54 +08:00
|
|
|
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.
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2013-05-28 16:32:54 +08:00
|
|
|
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.
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2013-05-28 16:32:54 +08:00
|
|
|
It is also possible to mark individual test instances within parametrize,
|
|
|
|
for example with the builtin ``mark.xfail``::
|
2013-05-21 09:12:45 +08:00
|
|
|
|
|
|
|
# content of test_expectation.py
|
|
|
|
import pytest
|
2013-05-28 16:32:54 +08:00
|
|
|
@pytest.mark.parametrize("input,expected", [
|
2013-05-21 09:12:45 +08:00
|
|
|
("3+5", 8),
|
|
|
|
("2+4", 6),
|
|
|
|
pytest.mark.xfail(("6*9", 42)),
|
|
|
|
])
|
|
|
|
def test_eval(input, expected):
|
|
|
|
assert eval(input) == expected
|
|
|
|
|
2013-05-28 16:32:54 +08:00
|
|
|
Let's run this::
|
|
|
|
|
|
|
|
$ py.test
|
2015-06-07 05:30:49 +08:00
|
|
|
======= test session starts ========
|
|
|
|
platform linux2 -- Python 2.7.9, pytest-2.8.0.dev4, py-1.4.28, pluggy-0.3.0
|
|
|
|
rootdir: $REGENDOC_TMPDIR, inifile:
|
2013-05-28 16:32:54 +08:00
|
|
|
collected 3 items
|
2014-01-29 20:47:11 +08:00
|
|
|
|
2013-05-28 16:32:54 +08:00
|
|
|
test_expectation.py ..x
|
2014-01-29 20:47:11 +08:00
|
|
|
|
2015-06-07 05:30:49 +08:00
|
|
|
======= 2 passed, 1 xfailed in 0.12 seconds ========
|
2013-05-28 16:32:54 +08:00
|
|
|
|
|
|
|
The one parameter set which caused a failure previously now
|
|
|
|
shows up as an "xfailed (expected to fail)" test.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
In versions prior to 2.4 one needed to specify the argument
|
|
|
|
names as a tuple. This remains valid but the simpler ``"name1,name2,..."``
|
2014-01-18 19:31:33 +08:00
|
|
|
comma-separated-string syntax is now advertised first because
|
2013-12-29 00:05:17 +08:00
|
|
|
it's easier to write and produces less line noise.
|
2012-10-07 19:06:17 +08:00
|
|
|
|
|
|
|
.. _`pytest_generate_tests`:
|
|
|
|
|
|
|
|
Basic ``pytest_generate_tests`` example
|
|
|
|
---------------------------------------------
|
|
|
|
|
2012-10-08 19:19:31 +08:00
|
|
|
Sometimes you may want to implement your own parametrization scheme
|
|
|
|
or implement some dynamism for determining the parameters or scope
|
2014-01-18 19:31:33 +08:00
|
|
|
of a fixture. For this, you can use the ``pytest_generate_tests`` hook
|
2012-10-09 20:35:17 +08:00
|
|
|
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
|
2014-01-18 19:31:33 +08:00
|
|
|
parametrization.
|
2012-10-09 20:35:17 +08:00
|
|
|
|
|
|
|
For example, let's say we want to run a test taking string inputs which
|
2014-01-18 19:31:33 +08:00
|
|
|
we want to set via a new ``pytest`` command line option. Let's first write
|
2012-10-09 20:35:17 +08:00
|
|
|
a simple test accepting a ``stringinput`` fixture function argument::
|
2012-10-08 19:19:31 +08:00
|
|
|
|
|
|
|
# content of test_strings.py
|
|
|
|
|
|
|
|
def test_valid_string(stringinput):
|
|
|
|
assert stringinput.isalpha()
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
Now we add a ``conftest.py`` file containing the addition of a
|
2012-10-08 19:19:31 +08:00
|
|
|
command line option and the parametrization of our test function::
|
2012-10-07 19:06:17 +08:00
|
|
|
|
|
|
|
# content of conftest.py
|
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
2012-10-09 20:35:17 +08:00
|
|
|
parser.addoption("--stringinput", action="append", default=[],
|
2012-10-08 19:19:31 +08:00
|
|
|
help="list of stringinputs to pass to test functions")
|
2012-10-07 19:06:17 +08:00
|
|
|
|
|
|
|
def pytest_generate_tests(metafunc):
|
2012-10-08 19:19:31 +08:00
|
|
|
if 'stringinput' in metafunc.fixturenames:
|
2014-01-18 19:31:33 +08:00
|
|
|
metafunc.parametrize("stringinput",
|
2012-10-08 19:19:31 +08:00
|
|
|
metafunc.config.option.stringinput)
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2012-10-08 19:19:31 +08:00
|
|
|
If we now pass two stringinput values, our test will run twice::
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2012-10-08 19:19:31 +08:00
|
|
|
$ py.test -q --stringinput="hello" --stringinput="world" test_strings.py
|
2012-10-07 19:06:17 +08:00
|
|
|
..
|
2015-06-07 05:30:49 +08:00
|
|
|
2 passed in 0.12 seconds
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
Let's also run with a stringinput that will lead to a failing test::
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2012-10-08 19:19:31 +08:00
|
|
|
$ py.test -q --stringinput="!" test_strings.py
|
|
|
|
F
|
2015-06-07 05:30:49 +08:00
|
|
|
======= FAILURES ========
|
|
|
|
_______ test_valid_string[!] ________
|
2014-01-29 20:47:11 +08:00
|
|
|
|
2012-10-08 19:19:31 +08:00
|
|
|
stringinput = '!'
|
2014-01-29 20:47:11 +08:00
|
|
|
|
2012-10-08 19:19:31 +08:00
|
|
|
def test_valid_string(stringinput):
|
|
|
|
> assert stringinput.isalpha()
|
2015-06-07 05:30:49 +08:00
|
|
|
E assert <built-in method isalpha of str object at 0xdeadbeef>()
|
|
|
|
E + where <built-in method isalpha of str object at 0xdeadbeef> = '!'.isalpha
|
2014-01-29 20:47:11 +08:00
|
|
|
|
2012-10-08 19:19:31 +08:00
|
|
|
test_strings.py:3: AssertionError
|
2015-06-07 05:30:49 +08:00
|
|
|
1 failed in 0.12 seconds
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
As expected our test function fails.
|
2012-10-09 20:35:17 +08:00
|
|
|
|
|
|
|
If you don't specify a stringinput it will be skipped because
|
|
|
|
``metafunc.parametrize()`` will be called with an empty parameter
|
|
|
|
listlist::
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
$ py.test -q -rs test_strings.py
|
2012-10-09 20:35:17 +08:00
|
|
|
s
|
2015-06-07 05:30:49 +08:00
|
|
|
======= short test summary info ========
|
|
|
|
SKIP [1] $PWD/_pytest/python.py:1201: got empty parameter set, function test_valid_string at $REGENDOC_TMPDIR/test_strings.py:1
|
|
|
|
1 skipped in 0.12 seconds
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2012-10-08 19:19:31 +08:00
|
|
|
For further examples, you might want to look at :ref:`more
|
|
|
|
parametrization examples <paramexamples>`.
|
2012-10-07 19:06:17 +08:00
|
|
|
|
|
|
|
.. _`metafunc object`:
|
|
|
|
|
|
|
|
The **metafunc** object
|
|
|
|
-------------------------------------------
|
|
|
|
|
|
|
|
.. currentmodule:: _pytest.python
|
|
|
|
|
|
|
|
metafunc objects are passed to the ``pytest_generate_tests`` hook.
|
|
|
|
They help to inspect a testfunction and to generate tests
|
|
|
|
according to test configuration or values specified
|
|
|
|
in the class or module where a test function is defined:
|
|
|
|
|
|
|
|
``metafunc.fixturenames``: set of required function arguments for given function
|
|
|
|
|
|
|
|
``metafunc.function``: underlying python test function
|
|
|
|
|
|
|
|
``metafunc.cls``: class object where the test function is defined in or None.
|
|
|
|
|
|
|
|
``metafunc.module``: the module object where the test function is defined in.
|
|
|
|
|
|
|
|
``metafunc.config``: access to command line opts and general config
|
|
|
|
|
|
|
|
``metafunc.funcargnames``: alias for ``fixturenames``, for pre-2.3 compatibility
|
|
|
|
|
|
|
|
.. automethod:: Metafunc.parametrize
|
|
|
|
.. automethod:: Metafunc.addcall(funcargs=None,id=_notexists,param=_notexists)
|
|
|
|
|
|
|
|
|