2010-11-21 04:35:55 +08:00
2011-02-09 21:55:21 +08:00
.. _paramexamples:
2011-09-06 17:43:42 +08:00
Parametrizing tests
2010-11-21 04:35:55 +08:00
=================================================
2011-11-17 19:09:21 +08:00
.. currentmodule:: _pytest.python
py.test allows to easily parametrize test functions.
In the following we provide some examples using
the builtin mechanisms.
.. _parametrizemark:
2011-12-05 18:10:48 +08:00
Simple "decorator" parametrization of a test function
2011-11-17 19:09:21 +08:00
----------------------------------------------------------------------------
.. versionadded:: 2.2
2011-12-05 18:10:48 +08:00
The builtin ``pytest.mark.parametrize`` decorator directly enables
2011-11-19 02:32:11 +08:00
parametrization of arguments for a test function. Here is an example
of a test function that wants to compare that processing some input
results in expected output::
2011-11-17 19:09:21 +08:00
# content of test_expectation.py
import pytest
@pytest.mark.parametrize(("input", "expected"), [
("3+5", 8),
("2+4", 6),
("6*9", 42),
])
def test_eval(input, expected):
assert eval(input) == expected
2011-12-05 18:10:48 +08:00
we parametrize two arguments of the test function so that the test
2011-11-17 19:09:21 +08:00
function is called three times. Let's run it::
$ py.test -q
collecting ... collected 3 items
..F
2012-07-23 16:54:57 +08:00
================================= FAILURES =================================
____________________________ test_eval[6*9-42] _____________________________
2011-11-17 19:09:21 +08:00
input = '6*9', expected = 42
@pytest.mark.parametrize(("input", "expected"), [
("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')
2011-11-19 02:32:11 +08:00
test_expectation.py:8: AssertionError
2012-07-14 18:06:58 +08:00
1 failed, 2 passed in 0.02 seconds
2011-11-17 19:09:21 +08:00
As expected only one pair of input/output values fails the simple test function.
Note that there are various ways how you can mark groups of functions,
see :ref:`mark`.
2010-11-21 04:35:55 +08:00
2011-09-06 17:43:42 +08:00
Generating parameters combinations, depending on command line
2011-02-09 21:55:21 +08:00
----------------------------------------------------------------------------
.. regendoc:wipe
2011-11-17 19:09:21 +08:00
Let's say we want to execute a test with different computation
parameters and the parameter range shall be determined by a command
line argument. Let's first write a simple (do-nothing) computation test::
2011-02-09 21:55:21 +08:00
# content of test_compute.py
def test_compute(param1):
assert param1 < 4
Now we add a test configuration like this::
# content of conftest.py
def pytest_addoption(parser):
parser.addoption("--all", action="store_true",
help="run all combinations")
def pytest_generate_tests(metafunc):
if 'param1' in metafunc.funcargnames:
if metafunc.config.option.all:
end = 5
else:
end = 2
2011-11-17 19:09:21 +08:00
metafunc.parametrize("param1", range(end))
2011-02-09 21:55:21 +08:00
This means that we only run 2 tests if we do not pass ``--all``::
$ py.test -q test_compute.py
collecting ... collected 2 items
..
2012-09-21 15:39:54 +08:00
2 passed in 0.01 seconds
2011-02-09 21:55:21 +08:00
We run only two computations, so we see two dots.
let's run the full monty::
$ py.test -q --all
collecting ... collected 5 items
....F
2012-07-23 16:54:57 +08:00
================================= FAILURES =================================
_____________________________ test_compute[4] ______________________________
2011-02-09 21:55:21 +08:00
param1 = 4
def test_compute(param1):
> assert param1 < 4
E assert 4 < 4
test_compute.py:3: AssertionError
2012-05-23 00:30:34 +08:00
1 failed, 4 passed in 0.02 seconds
2011-02-09 21:55:21 +08:00
As expected when running the full range of ``param1`` values
we'll get an error on the last one.
2011-12-05 18:10:48 +08:00
A quick port of "testscenarios"
2011-11-17 19:09:21 +08:00
------------------------------------
2012-07-02 19:13:48 +08:00
.. _`test scenarios`: http://pypi.python.org/pypi/testscenarios/
2011-11-17 19:09:21 +08:00
2011-12-05 18:10:48 +08:00
Here is a quick port to run tests configured with `test scenarios`_,
2011-11-17 19:09:21 +08:00
an add-on from Robert Collins for the standard unittest framework. We
only have to work a bit to construct the correct arguments for pytest's
:py:func:`Metafunc.parametrize`::
# content of test_scenarios.py
def pytest_generate_tests(metafunc):
idlist = []
argvalues = []
for scenario in metafunc.cls.scenarios:
idlist.append(scenario[0])
items = scenario[1].items()
argnames = [x[0] for x in items]
argvalues.append(([x[1] for x in items]))
2012-09-21 15:39:54 +08:00
metafunc.parametrize(argnames, argvalues, ids=idlist, scope="class")
2011-11-17 19:09:21 +08:00
scenario1 = ('basic', {'attribute': 'value'})
scenario2 = ('advanced', {'attribute': 'value2'})
class TestSampleWithScenarios:
scenarios = [scenario1, scenario2]
2012-09-21 15:39:54 +08:00
def test_demo1(self, attribute):
assert isinstance(attribute, str)
def test_demo2(self, attribute):
2011-11-17 19:09:21 +08:00
assert isinstance(attribute, str)
this is a fully self-contained example which you can run with::
$ py.test test_scenarios.py
2012-07-23 16:54:57 +08:00
=========================== test session starts ============================
2012-09-21 15:39:54 +08:00
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev14
plugins: xdist, bugzilla, cache, oejskit, cli, timeout, pep8, cov
collecting ... collected 4 items
2011-11-17 19:09:21 +08:00
2012-09-21 15:39:54 +08:00
test_scenarios.py ....
2011-11-17 19:09:21 +08:00
2012-09-21 15:39:54 +08:00
========================= 4 passed in 0.02 seconds =========================
2011-11-17 19:09:21 +08:00
If you just collect tests you'll also nicely see 'advanced' and 'basic' as variants for the test function::
$ py.test --collectonly test_scenarios.py
2012-07-23 16:54:57 +08:00
=========================== test session starts ============================
2012-09-21 15:39:54 +08:00
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev14
plugins: xdist, bugzilla, cache, oejskit, cli, timeout, pep8, cov
collecting ... collected 4 items
2011-11-17 19:09:21 +08:00
<Module 'test_scenarios.py'>
<Class 'TestSampleWithScenarios'>
<Instance '()'>
2012-09-21 15:39:54 +08:00
<Function 'test_demo1[basic]'>
<Function 'test_demo2[basic]'>
<Function 'test_demo1[advanced]'>
<Function 'test_demo2[advanced]'>
2011-11-17 19:09:21 +08:00
2012-09-21 15:39:54 +08:00
============================= in 0.01 seconds =============================
Note that we told ``metafunc.parametrize()`` that your scenario values
should be considered class-scoped. With pytest-2.3 this leads to a
resource-based ordering.
2011-11-17 19:09:21 +08:00
Deferring the setup of parametrized resources
2011-02-09 21:55:21 +08:00
---------------------------------------------------
.. regendoc:wipe
The parametrization of test functions happens at collection
2011-11-17 19:09:21 +08:00
time. It is a good idea to setup expensive resources like DB
2011-12-05 18:10:48 +08:00
connections or subprocess only when the actual test is run.
Here is a simple example how you can achieve that, first
2011-11-17 19:09:21 +08:00
the actual test requiring a ``db`` object::
2011-02-09 21:55:21 +08:00
# content of test_backends.py
2011-12-05 18:10:48 +08:00
2011-02-09 21:55:21 +08:00
import pytest
def test_db_initialized(db):
# a dummy test
if db.__class__.__name__ == "DB2":
pytest.fail("deliberately failing for demo purposes")
2011-11-17 19:09:21 +08:00
We can now add a test configuration that generates two invocations of
the ``test_db_initialized`` function and also implements a factory that
creates a database object for the actual test invocations::
2011-02-09 21:55:21 +08:00
# content of conftest.py
def pytest_generate_tests(metafunc):
if 'db' in metafunc.funcargnames:
2011-11-17 19:09:21 +08:00
metafunc.parametrize("db", ['d1', 'd2'], indirect=True)
2011-02-09 21:55:21 +08:00
class DB1:
"one database object"
class DB2:
"alternative database object"
2011-12-05 18:10:48 +08:00
2011-02-09 21:55:21 +08:00
def pytest_funcarg__db(request):
if request.param == "d1":
return DB1()
elif request.param == "d2":
return DB2()
else:
raise ValueError("invalid internal test config")
Let's first see how it looks like at collection time::
$ py.test test_backends.py --collectonly
2012-07-23 16:54:57 +08:00
=========================== test session starts ============================
2012-09-21 15:39:54 +08:00
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev14
plugins: xdist, bugzilla, cache, oejskit, cli, timeout, pep8, cov
2011-03-09 17:58:36 +08:00
collecting ... collected 2 items
2011-02-09 21:55:21 +08:00
<Module 'test_backends.py'>
2011-11-17 19:09:21 +08:00
<Function 'test_db_initialized[d1]'>
<Function 'test_db_initialized[d2]'>
2011-03-09 17:58:36 +08:00
2012-09-21 15:39:54 +08:00
============================= in 0.01 seconds =============================
2011-02-09 21:55:21 +08:00
And then when we run the test::
$ py.test -q test_backends.py
collecting ... collected 2 items
.F
2012-07-23 16:54:57 +08:00
================================= FAILURES =================================
_________________________ test_db_initialized[d2] __________________________
2011-02-09 21:55:21 +08:00
2012-09-21 15:39:54 +08:00
db = <conftest.DB2 instance at 0x17dd440>
2011-02-09 21:55:21 +08:00
def test_db_initialized(db):
# a dummy test
if db.__class__.__name__ == "DB2":
> pytest.fail("deliberately failing for demo purposes")
E Failed: deliberately failing for demo purposes
test_backends.py:6: Failed
2012-09-21 15:39:54 +08:00
1 failed, 1 passed in 0.01 seconds
2011-02-09 21:55:21 +08:00
2011-11-17 19:09:21 +08:00
The first invocation with ``db == "DB1"`` passed while the second with ``db == "DB2"`` failed. Our ``pytest_funcarg__db`` factory has instantiated each of the DB values during the setup phase while the ``pytest_generate_tests`` generated two according calls to the ``test_db_initialized`` during the collection phase.
.. regendoc:wipe
2011-02-09 21:55:21 +08:00
2010-11-21 04:35:55 +08:00
Parametrizing test methods through per-class configuration
--------------------------------------------------------------
.. _`unittest parameterizer`: http://code.google.com/p/unittest-ext/source/browse/trunk/params.py
2011-11-17 19:09:21 +08:00
2010-11-21 04:35:55 +08:00
Here is an example ``pytest_generate_function`` function implementing a
2011-12-05 18:10:48 +08:00
parametrization scheme similar to Michael Foord's `unittest
2011-11-17 19:09:21 +08:00
parameterizer`_ but in a lot less code::
2010-11-21 04:35:55 +08:00
# content of ./test_parametrize.py
import pytest
def pytest_generate_tests(metafunc):
# called once per each test function
2011-11-17 19:09:21 +08:00
funcarglist = metafunc.cls.params[metafunc.function.__name__]
argnames = list(funcarglist[0])
2011-12-05 18:10:48 +08:00
metafunc.parametrize(argnames, [[funcargs[name] for name in argnames]
2011-11-17 19:09:21 +08:00
for funcargs in funcarglist])
2010-11-21 04:35:55 +08:00
class TestClass:
# a map specifying multiple argument sets for a test method
params = {
'test_equals': [dict(a=1, b=2), dict(a=3, b=3), ],
2011-11-17 19:09:21 +08:00
'test_zerodivision': [dict(a=1, b=0), ],
2010-11-21 04:35:55 +08:00
}
def test_equals(self, a, b):
assert a == b
def test_zerodivision(self, a, b):
pytest.raises(ZeroDivisionError, "a/b")
2011-11-17 19:09:21 +08:00
Our test generator looks up a class-level definition which specifies which
argument sets to use for each test function. Let's run it::
2010-11-21 04:35:55 +08:00
$ py.test -q
2011-11-17 19:09:21 +08:00
collecting ... collected 3 items
F..
2012-07-23 16:54:57 +08:00
================================= FAILURES =================================
________________________ TestClass.test_equals[1-2] ________________________
2010-11-21 04:35:55 +08:00
2012-09-21 15:39:54 +08:00
self = <test_parametrize.TestClass instance at 0x19a6d88>, a = 1, b = 2
2010-11-21 04:35:55 +08:00
def test_equals(self, a, b):
> assert a == b
E assert 1 == 2
2011-11-17 19:09:21 +08:00
test_parametrize.py:18: AssertionError
2012-07-14 18:06:58 +08:00
1 failed, 2 passed in 0.02 seconds
2010-11-21 04:35:55 +08:00
2011-11-19 02:32:11 +08:00
Indirect parametrization with multiple resources
2010-11-21 04:35:55 +08:00
--------------------------------------------------------------
Here is a stripped down real-life example of using parametrized
2011-11-17 19:09:21 +08:00
testing for testing serialization, invoking different python interpreters.
2010-11-21 04:35:55 +08:00
We define a ``test_basic_objects`` function which is to be run
2011-11-19 02:32:11 +08:00
with different sets of arguments for its three arguments:
2010-11-21 04:35:55 +08:00
2011-11-17 19:09:21 +08:00
* ``python1``: first python interpreter, run to pickle-dump an object to a file
2011-12-05 18:10:48 +08:00
* ``python2``: second interpreter, run to pickle-load an object from a file
2011-11-17 19:09:21 +08:00
* ``obj``: object to be dumped/loaded
2010-11-21 04:35:55 +08:00
.. literalinclude:: multipython.py
2011-11-19 02:32:11 +08:00
Running it results in some skips if we don't have all the python interpreters installed and otherwise runs all combinations (5 interpreters times 5 interpreters times 3 objects to serialize/deserialize)::
2010-11-21 04:35:55 +08:00
2011-11-19 02:32:11 +08:00
. $ py.test -rs -q multipython.py
2010-11-26 20:26:56 +08:00
collecting ... collected 75 items
2012-05-23 00:30:34 +08:00
............sss............sss............sss............ssssssssssssssssss
2012-07-23 16:54:57 +08:00
========================= short test summary info ==========================
2012-09-21 15:39:54 +08:00
SKIP [27] /home/hpk/p/pytest/doc/en/example/multipython.py:21: 'python2.8' not found
48 passed, 27 skipped in 3.11 seconds