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
=================================================
2014-01-18 19:31:33 +08:00
`` pytest `` allows to easily parametrize test functions.
2012-10-18 18:24:50 +08:00
For basic docs, see :ref: `parametrize-basics` .
2011-11-17 19:09:21 +08:00
In the following we provide some examples using
the builtin mechanisms.
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
2019-08-07 07:20:06 +08:00
line argument. Let's first write a simple (do-nothing) computation test:
2011-02-09 21:55:21 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: python
2011-02-09 21:55:21 +08:00
# content of test_compute.py
2019-08-07 04:34:58 +08:00
2011-02-09 21:55:21 +08:00
def test_compute(param1):
assert param1 < 4
2019-08-07 07:20:06 +08:00
Now we add a test configuration like this:
2011-02-09 21:55:21 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: python
2011-02-09 21:55:21 +08:00
# content of conftest.py
2019-08-07 04:34:58 +08:00
2011-02-09 21:55:21 +08:00
def pytest_addoption(parser):
parser.addoption("--all", action="store_true", help="run all combinations")
2019-08-07 04:34:58 +08:00
2011-02-09 21:55:21 +08:00
def pytest_generate_tests(metafunc):
2012-10-05 20:24:44 +08:00
if "param1" in metafunc.fixturenames:
2017-07-28 06:18:44 +08:00
if metafunc.config.getoption("all"):
2011-02-09 21:55:21 +08:00
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
2018-11-24 13:41:22 +08:00
This means that we only run 2 tests if we do not pass `` --all `` :
.. code-block :: pytest
2011-02-09 21:55:21 +08:00
2016-06-21 22:16:57 +08:00
$ pytest -q test_compute.py
2019-01-06 03:19:40 +08:00
.. [100%]
2019-09-18 21:11:59 +08:00
2 passed in 0.12s
2011-02-09 21:55:21 +08:00
We run only two computations, so we see two dots.
2018-11-24 13:41:22 +08:00
let's run the full monty:
.. code-block :: pytest
2011-02-09 21:55:21 +08:00
2016-06-21 22:16:57 +08:00
$ pytest -q --all
2019-01-06 03:19:40 +08:00
....F [100%]
================================= FAILURES =================================
_____________________________ test_compute[4] ______________________________
2018-05-18 16:19:46 +08:00
2011-02-09 21:55:21 +08:00
param1 = 4
2018-05-18 16:19:46 +08:00
2011-02-09 21:55:21 +08:00
def test_compute(param1):
> assert param1 < 4
E assert 4 < 4
2018-05-18 16:19:46 +08:00
2019-08-16 08:00:09 +08:00
test_compute.py:4: AssertionError
2020-03-11 22:23:25 +08:00
========================= short test summary info ==========================
FAILED test_compute.py::test_compute[4] - assert 4 < 4
2019-09-18 21:11:59 +08:00
1 failed, 4 passed in 0.12s
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.
2014-04-18 03:08:49 +08:00
Different options for test IDs
------------------------------------
pytest will build a string that is the test ID for each set of values in a
2014-10-08 07:43:27 +08:00
parametrized test. These IDs can be used with `` -k `` to select specific cases
2014-04-18 03:08:49 +08:00
to run, and they will also identify the specific case when one is failing.
2014-10-08 07:43:27 +08:00
Running pytest with `` --collect-only `` will show the generated IDs.
2014-04-18 03:08:49 +08:00
Numbers, strings, booleans and None will have their usual string representation
used in the test ID. For other objects, pytest will make a string based on
2019-08-07 07:20:06 +08:00
the argument name:
2014-04-18 03:08:49 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: python
2015-07-25 23:48:12 +08:00
# content of test_time.py
2014-04-18 03:08:49 +08:00
from datetime import datetime, timedelta
2021-01-19 22:45:45 +08:00
import pytest
2015-09-26 00:42:06 +08:00
testdata = [
(datetime(2001, 12, 12), datetime(2001, 12, 11), timedelta(1)),
(datetime(2001, 12, 11), datetime(2001, 12, 12), timedelta(-1)),
]
2014-04-18 03:08:49 +08:00
@pytest.mark.parametrize("a,b,expected", testdata)
def test_timedistance_v0(a, b, expected):
2015-09-26 00:42:06 +08:00
diff = a - b
assert diff == expected
2014-04-18 03:08:49 +08:00
@pytest.mark.parametrize("a,b,expected", testdata, ids=["forward", "backward"])
def test_timedistance_v1(a, b, expected):
2015-09-26 00:42:06 +08:00
diff = a - b
assert diff == expected
2014-04-18 03:08:49 +08:00
def idfn(val):
2015-09-26 00:42:06 +08:00
if isinstance(val, (datetime,)):
# note this wouldn't show any hours/minutes/seconds
return val.strftime("%Y%m%d")
2014-04-18 03:08:49 +08:00
@pytest.mark.parametrize("a,b,expected", testdata, ids=idfn)
def test_timedistance_v2(a, b, expected):
2015-09-26 00:42:06 +08:00
diff = a - b
assert diff == expected
2014-04-18 03:08:49 +08:00
2019-08-07 04:34:58 +08:00
2017-05-23 13:57:34 +08:00
@pytest.mark.parametrize(
"a,b,expected",
[
pytest.param(
datetime(2001, 12, 12), datetime(2001, 12, 11), timedelta(1), id="forward"
),
pytest.param(
datetime(2001, 12, 11), datetime(2001, 12, 12), timedelta(-1), id="backward"
),
],
)
def test_timedistance_v3(a, b, expected):
diff = a - b
assert diff == expected
2014-04-18 03:08:49 +08:00
In `` test_timedistance_v0 `` , we let pytest generate the test IDs.
In `` test_timedistance_v1 `` , we specified `` ids `` as a list of strings which were
used as the test IDs. These are succinct, but can be a pain to maintain.
In `` test_timedistance_v2 `` , we specified `` ids `` as a function that can generate a
string representation to make part of the test ID. So our `` datetime `` values use the
label generated by `` idfn `` , but because we didn't generate a label for `` timedelta ``
2018-11-24 13:41:22 +08:00
objects, they are still using the default pytest representation:
2014-04-18 03:08:49 +08:00
2018-11-24 13:41:22 +08:00
.. code-block :: pytest
2014-04-18 03:08:49 +08:00
2016-06-21 22:16:57 +08:00
$ pytest test_time.py --collect-only
2019-01-06 03:19:40 +08:00
=========================== test session starts ============================
2024-01-02 16:58:20 +08:00
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
2021-10-04 14:56:26 +08:00
rootdir: /home/sweet/project
2017-05-23 13:57:34 +08:00
collected 8 items
2020-07-09 05:51:01 +08:00
2024-01-28 05:55:26 +08:00
<Dir parametrize.rst-193>
2024-01-02 16:58:20 +08:00
<Module test_time.py>
<Function test_timedistance_v0[a0-b0-expected0]>
<Function test_timedistance_v0[a1-b1-expected1]>
<Function test_timedistance_v1[forward]>
<Function test_timedistance_v1[backward]>
<Function test_timedistance_v2[20011212-20011211-expected0]>
<Function test_timedistance_v2[20011211-20011212-expected1]>
<Function test_timedistance_v3[forward]>
<Function test_timedistance_v3[backward]>
2018-05-18 16:19:46 +08:00
2020-12-13 05:21:28 +08:00
======================== 8 tests collected in 0.12s ========================
2014-04-18 03:08:49 +08:00
2017-05-23 13:57:34 +08:00
In `` test_timedistance_v3 `` , we used `` pytest.param `` to specify the test IDs
together with the actual data, instead of listing them separately.
2011-12-05 18:10:48 +08:00
A quick port of "testscenarios"
2011-11-17 19:09:21 +08:00
------------------------------------
2021-10-22 20:47:57 +08:00
Here is a quick port to run tests configured with :pypi: `testscenarios` ,
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
2023-12-07 05:37:52 +08:00
:py:func: `Metafunc.parametrize <pytest.Metafunc.parametrize>` :
2011-11-17 19:09:21 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: python
2011-11-17 19:09:21 +08:00
# content of test_scenarios.py
2019-08-07 04:34:58 +08:00
2011-11-17 19:09:21 +08:00
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
2019-08-07 04:34:58 +08:00
2011-11-17 19:09:21 +08:00
scenario1 = ("basic", {"attribute": "value"})
scenario2 = ("advanced", {"attribute": "value2"})
2019-08-07 04:34:58 +08:00
2011-11-17 19:09:21 +08:00
2017-02-17 02:41:51 +08:00
class TestSampleWithScenarios:
2011-11-17 19:09:21 +08:00
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)
2018-11-24 13:41:22 +08:00
this is a fully self-contained example which you can run with:
.. code-block :: pytest
2011-11-17 19:09:21 +08:00
2016-06-21 22:16:57 +08:00
$ pytest test_scenarios.py
2019-01-06 03:19:40 +08:00
=========================== test session starts ============================
2024-01-02 16:58:20 +08:00
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
2021-10-04 14:56:26 +08:00
rootdir: /home/sweet/project
2012-10-07 19:06:17 +08:00
collected 4 items
2018-05-18 16:19:46 +08:00
2019-01-06 03:19:40 +08:00
test_scenarios.py .... [100%]
2018-05-18 16:19:46 +08:00
2019-08-30 23:43:47 +08:00
============================ 4 passed in 0.12s =============================
2011-11-17 19:09:21 +08:00
2018-11-24 13:41:22 +08:00
If you just collect tests you'll also nicely see 'advanced' and 'basic' as variants for the test function:
2011-11-17 19:09:21 +08:00
2018-11-24 13:41:22 +08:00
.. code-block :: pytest
2011-11-17 19:09:21 +08:00
2016-06-21 22:16:57 +08:00
$ pytest --collect-only test_scenarios.py
2019-01-06 03:19:40 +08:00
=========================== test session starts ============================
2024-01-02 16:58:20 +08:00
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
2021-10-04 14:56:26 +08:00
rootdir: /home/sweet/project
2012-10-07 19:06:17 +08:00
collected 4 items
2020-07-09 05:51:01 +08:00
2024-01-28 05:55:26 +08:00
<Dir parametrize.rst-193>
2024-01-02 16:58:20 +08:00
<Module test_scenarios.py>
<Class TestSampleWithScenarios>
<Function test_demo1[basic]>
<Function test_demo2[basic]>
<Function test_demo1[advanced]>
<Function test_demo2[advanced]>
2018-05-18 16:19:46 +08:00
2020-12-13 05:21:28 +08:00
======================== 4 tests collected in 0.12s ========================
2012-09-21 15:39:54 +08:00
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.
2019-08-16 00:12:18 +08:00
Here is a simple example how you can achieve that. This test
requires a `` db `` object fixture:
2011-02-09 21:55:21 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: python
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
2019-08-07 04:34:58 +08:00
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")
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
2019-08-07 07:20:06 +08:00
creates a database object for the actual test invocations:
2011-02-09 21:55:21 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: python
2011-02-09 21:55:21 +08:00
# content of conftest.py
2012-10-18 18:24:50 +08:00
import pytest
2011-02-09 21:55:21 +08:00
2019-08-07 04:34:58 +08:00
2011-02-09 21:55:21 +08:00
def pytest_generate_tests(metafunc):
2012-10-05 20:24:44 +08:00
if "db" in metafunc.fixturenames:
2011-11-17 19:09:21 +08:00
metafunc.parametrize("db", ["d1", "d2"], indirect=True)
2019-08-07 04:34:58 +08:00
2011-02-09 21:55:21 +08:00
2017-02-17 02:41:51 +08:00
class DB1:
2011-02-09 21:55:21 +08:00
"one database object"
2019-08-07 04:34:58 +08:00
2017-02-17 02:41:51 +08:00
class DB2:
2011-02-09 21:55:21 +08:00
"alternative database object"
2011-12-05 18:10:48 +08:00
2019-08-07 04:34:58 +08:00
2012-10-18 18:24:50 +08:00
@pytest.fixture
def db(request):
2011-02-09 21:55:21 +08:00
if request.param == "d1":
return DB1()
elif request.param == "d2":
return DB2()
else:
raise ValueError("invalid internal test config")
2018-11-24 13:41:22 +08:00
Let's first see how it looks like at collection time:
.. code-block :: pytest
2011-02-09 21:55:21 +08:00
2016-06-21 22:16:57 +08:00
$ pytest test_backends.py --collect-only
2019-01-06 03:19:40 +08:00
=========================== test session starts ============================
2024-01-02 16:58:20 +08:00
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
2021-10-04 14:56:26 +08:00
rootdir: /home/sweet/project
2012-10-07 19:06:17 +08:00
collected 2 items
2020-07-09 05:51:01 +08:00
2024-01-28 05:55:26 +08:00
<Dir parametrize.rst-193>
2024-01-02 16:58:20 +08:00
<Module test_backends.py>
<Function test_db_initialized[d1]>
<Function test_db_initialized[d2]>
2018-05-18 16:19:46 +08:00
2020-12-13 05:21:28 +08:00
======================== 2 tests collected in 0.12s ========================
2011-02-09 21:55:21 +08:00
2018-11-24 13:41:22 +08:00
And then when we run the test:
.. code-block :: pytest
2011-02-09 21:55:21 +08:00
2016-06-21 22:16:57 +08:00
$ pytest -q test_backends.py
2019-01-06 03:19:40 +08:00
.F [100%]
================================= FAILURES =================================
_________________________ test_db_initialized[d2] __________________________
2018-05-18 16:19:46 +08:00
2021-10-04 14:56:26 +08:00
db = <conftest.DB2 object at 0xdeadbeef0001>
2018-05-18 16:19:46 +08:00
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
2018-05-18 16:19:46 +08:00
2019-08-16 08:00:09 +08:00
test_backends.py:8: Failed
2020-03-11 22:23:25 +08:00
========================= short test summary info ==========================
FAILED test_backends.py::test_db_initialized[d2] - Failed: deliberately f...
2019-09-18 21:11:59 +08:00
1 failed, 1 passed in 0.12s
2011-02-09 21:55:21 +08:00
2012-10-18 18:24:50 +08:00
The first invocation with `` db == "DB1" `` passed while the second with `` db == "DB2" `` failed. Our `` db `` fixture function 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.
2011-11-17 19:09:21 +08:00
2020-06-10 19:08:27 +08:00
Indirect parametrization
---------------------------------------------------
Using the `` indirect=True `` parameter when parametrizing a test allows to
parametrize a test with a fixture receiving the values before passing them to a
test:
.. code-block :: python
import pytest
@pytest.fixture
def fixt(request):
return request.param * 3
@pytest.mark.parametrize("fixt", ["a", "b"], indirect=True)
def test_indirect(fixt):
assert len(fixt) == 3
This can be used, for example, to do more expensive setup at test run time in
the fixture, rather than having to run those setup steps at collection time.
2011-11-17 19:09:21 +08:00
.. regendoc:wipe
2011-02-09 21:55:21 +08:00
2015-08-04 05:48:41 +08:00
Apply indirect on particular arguments
---------------------------------------------------
Very often parametrization uses more than one argument name. There is opportunity to apply `` indirect ``
parameter on particular arguments. It can be done by passing list or tuple of
arguments' names to `` indirect `` . In the example below there is a function `` test_indirect `` which uses
two fixtures: `` x `` and `` y `` . Here we give to indirect the list, which contains the name of the
fixture `` x `` . The indirect parameter will be applied to this argument only, and the value `` a ``
2019-08-07 07:20:06 +08:00
will be passed to respective fixture function:
2015-08-04 05:48:41 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: python
2015-08-04 05:48:41 +08:00
# content of test_indirect_list.py
import pytest
2019-08-07 04:34:58 +08:00
2015-08-04 05:48:41 +08:00
@pytest.fixture(scope="function")
def x(request):
return request.param * 3
2019-08-07 04:34:58 +08:00
2015-08-04 05:48:41 +08:00
@pytest.fixture(scope="function")
def y(request):
return request.param * 2
2019-08-07 04:34:58 +08:00
2015-08-04 05:48:41 +08:00
@pytest.mark.parametrize("x, y", [("a", "b")], indirect=["x"])
def test_indirect(x, y):
assert x == "aaa"
assert y == "b"
2018-11-24 13:41:22 +08:00
The result of this test will be successful:
.. code-block :: pytest
2015-08-04 05:48:41 +08:00
2020-05-11 19:26:16 +08:00
$ pytest -v test_indirect_list.py
2019-01-06 03:19:40 +08:00
=========================== test session starts ============================
2024-01-02 16:58:20 +08:00
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y -- $PYTHON_PREFIX/bin/python
2021-10-04 14:56:26 +08:00
cachedir: .pytest_cache
rootdir: /home/sweet/project
2020-07-09 05:51:01 +08:00
collecting ... collected 1 item
2020-05-11 19:26:16 +08:00
2020-07-09 05:51:01 +08:00
test_indirect_list.py::test_indirect[a-b] PASSED [100%]
2020-05-11 19:26:16 +08:00
2020-07-09 05:51:01 +08:00
============================ 1 passed in 0.12s =============================
2015-08-04 05:48:41 +08:00
.. regendoc:wipe
2010-11-21 04:35:55 +08:00
Parametrizing test methods through per-class configuration
--------------------------------------------------------------
2015-12-27 09:35:02 +08:00
.. _`unittest parametrizer`: https://github.com/testing-cabal/unittest-ext/blob/master/params.py
2010-11-21 04:35:55 +08:00
2011-11-17 19:09:21 +08:00
2017-10-16 06:55:30 +08:00
Here is an example `` pytest_generate_tests `` function implementing a
2011-12-05 18:10:48 +08:00
parametrization scheme similar to Michael Foord's `unittest
2019-08-07 07:20:06 +08:00
parametrizer`_ but in a lot less code:
2010-11-21 04:35:55 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: python
2010-11-21 04:35:55 +08:00
# content of ./test_parametrize.py
import pytest
2019-08-07 04:34:58 +08:00
2010-11-21 04:35:55 +08:00
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__]
2016-08-04 04:48:11 +08:00
argnames = sorted(funcarglist[0])
2011-12-05 18:10:48 +08:00
metafunc.parametrize(
argnames, [[funcargs[name] for name in argnames] for funcargs in funcarglist]
2011-11-17 19:09:21 +08:00
)
2019-08-07 04:34:58 +08:00
2010-11-21 04:35:55 +08:00
2017-02-17 02:41:51 +08:00
class TestClass:
2010-11-21 04:35:55 +08:00
# 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):
2018-11-23 02:05:10 +08:00
with pytest.raises(ZeroDivisionError):
a / b
2010-11-21 04:35:55 +08:00
2011-11-17 19:09:21 +08:00
Our test generator looks up a class-level definition which specifies which
2018-11-24 13:41:22 +08:00
argument sets to use for each test function. Let's run it:
.. code-block :: pytest
2010-11-21 04:35:55 +08:00
2016-06-21 22:16:57 +08:00
$ pytest -q
2019-01-06 03:19:40 +08:00
F.. [100%]
================================= FAILURES =================================
________________________ TestClass.test_equals[1-2] ________________________
2018-05-18 16:19:46 +08:00
2021-10-04 14:56:26 +08:00
self = <test_parametrize.TestClass object at 0xdeadbeef0002>, a = 1, b = 2
2018-05-18 16:19:46 +08:00
2010-11-21 04:35:55 +08:00
def test_equals(self, a, b):
> assert a == b
E assert 1 == 2
2018-05-18 16:19:46 +08:00
2019-08-16 08:00:09 +08:00
test_parametrize.py:21: AssertionError
2020-03-11 22:23:25 +08:00
========================= short test summary info ==========================
FAILED test_parametrize.py::TestClass::test_equals[1-2] - assert 1 == 2
2019-09-18 21:11:59 +08:00
1 failed, 2 passed in 0.12s
2010-11-21 04:35:55 +08:00
2023-07-20 14:41:16 +08:00
Parametrization with multiple fixtures
--------------------------------------
2010-11-21 04:35:55 +08:00
Here is a stripped down real-life example of using parametrized
2012-10-18 18:24:50 +08:00
testing for testing serialization of objects between different python
interpreters. We define a `` test_basic_objects `` function which
is to be run 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
2019-06-03 04:55:28 +08:00
Running it results in some skips if we don't have all the python interpreters installed and otherwise runs all combinations (3 interpreters times 3 interpreters times 3 objects to serialize/deserialize):
2018-11-24 13:41:22 +08:00
.. code-block :: pytest
2010-11-21 04:35:55 +08:00
2016-06-21 22:16:57 +08:00
. $ pytest -rs -q multipython.py
2024-02-17 06:09:47 +08:00
ssssssssssss...ssssssssssss [100%]
2023-04-09 05:49:26 +08:00
========================= short test summary info ==========================
2024-02-17 06:09:47 +08:00
SKIPPED [12] multipython.py:65: 'python3.9' not found
SKIPPED [12] multipython.py:65: 'python3.11' not found
2024-01-02 16:58:20 +08:00
3 passed, 24 skipped in 0.12s
2012-12-20 22:57:07 +08:00
2023-07-20 14:41:16 +08:00
Parametrization of optional implementations/imports
---------------------------------------------------
2012-12-20 22:57:07 +08:00
If you want to compare the outcomes of several implementations of a given
API, you can write test functions that receive the already imported implementations
and get skipped in case the implementation is not importable/available. Let's
2014-01-18 19:31:33 +08:00
say we have a "base" implementation and the other (possibly optimized ones)
2019-08-07 07:20:06 +08:00
need to provide similar results:
2012-12-20 22:57:07 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: python
2012-12-20 22:57:07 +08:00
# content of conftest.py
import pytest
2019-08-07 04:34:58 +08:00
2012-12-20 22:57:07 +08:00
@pytest.fixture(scope="session")
def basemod(request):
return pytest.importorskip("base")
2019-08-07 04:34:58 +08:00
2012-12-20 22:57:07 +08:00
@pytest.fixture(scope="session", params=["opt1", "opt2"])
def optmod(request):
return pytest.importorskip(request.param)
2019-08-07 07:20:06 +08:00
And then a base implementation of a simple function:
2012-12-20 22:57:07 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: python
2012-12-20 22:57:07 +08:00
# content of base.py
def func1():
return 1
2019-08-07 07:20:06 +08:00
And an optimized version:
2012-12-20 22:57:07 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: python
2012-12-20 22:57:07 +08:00
# content of opt1.py
def func1():
return 1.0001
2019-08-07 07:20:06 +08:00
And finally a little test module:
2012-12-20 22:57:07 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: python
2012-12-20 22:57:07 +08:00
# content of test_module.py
2019-08-07 04:34:58 +08:00
2012-12-20 22:57:07 +08:00
def test_func1(basemod, optmod):
assert round(basemod.func1(), 3) == round(optmod.func1(), 3)
2018-11-24 13:41:22 +08:00
If you run this with reporting for skips enabled:
.. code-block :: pytest
2012-12-20 22:57:07 +08:00
2016-06-21 22:16:57 +08:00
$ pytest -rs test_module.py
2019-01-06 03:19:40 +08:00
=========================== test session starts ============================
2024-01-02 16:58:20 +08:00
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
2021-10-04 14:56:26 +08:00
rootdir: /home/sweet/project
2012-12-20 22:57:07 +08:00
collected 2 items
2018-05-18 16:19:46 +08:00
2019-01-06 03:19:40 +08:00
test_module.py .s [100%]
2019-05-09 05:50:08 +08:00
2019-01-06 03:19:40 +08:00
========================= short test summary info ==========================
2023-01-14 20:20:20 +08:00
SKIPPED [1] test_module.py:3: could not import 'opt2': No module named 'opt2'
2019-08-30 23:43:47 +08:00
======================= 1 passed, 1 skipped in 0.12s =======================
2012-12-20 22:57:07 +08:00
2018-05-13 18:06:09 +08:00
You'll see that we don't have an `` opt2 `` module and thus the second test run
2012-12-20 22:57:07 +08:00
of our `` test_func1 `` was skipped. A few notes:
- the fixture functions in the `` conftest.py `` file are "session-scoped" because we
2014-01-18 19:31:33 +08:00
don't need to import more than once
2012-12-20 22:57:07 +08:00
- if you have multiple test functions and a skipped import, you will see
the `` [1] `` count increasing in the report
- you can put :ref: `@pytest.mark.parametrize <@pytest.mark.parametrize>` style
2014-01-18 19:31:33 +08:00
parametrization on the test functions to parametrize input/output
2012-12-20 22:57:07 +08:00
values as well.
2017-11-04 02:37:18 +08:00
Set marks or test ID for individual parametrized test
--------------------------------------------------------------------
Use `` pytest.param `` to apply marks or set test ID to individual parametrized test.
2019-04-12 19:50:26 +08:00
For example:
.. code-block :: python
2017-11-04 02:37:18 +08:00
# content of test_pytest_param_example.py
import pytest
2019-04-12 19:50:26 +08:00
@pytest.mark.parametrize(
"test_input,expected",
[
("3+5", 8),
pytest.param("1+7", 8, marks=pytest.mark.basic),
pytest.param("2+4", 6, marks=pytest.mark.basic, id="basic_2+4"),
pytest.param(
"6*9", 42, marks=[pytest.mark.basic, pytest.mark.xfail], id="basic_6* 9"
),
],
)
2017-11-04 02:37:18 +08:00
def test_eval(test_input, expected):
assert eval(test_input) == expected
2018-05-18 16:19:46 +08:00
2017-11-04 02:37:18 +08:00
In this example, we have 4 parametrized tests. Except for the first test,
we mark the rest three parametrized tests with the custom marker `` basic `` ,
2018-05-18 16:19:46 +08:00
and for the fourth test we also use the built-in mark `` xfail `` to indicate this
2017-11-04 02:37:18 +08:00
test is expected to fail. For explicitness, we set test ids for some tests.
2018-11-24 13:41:22 +08:00
Then run `` pytest `` with verbose mode and with only the `` basic `` marker:
2017-11-04 02:37:18 +08:00
2018-11-24 13:41:22 +08:00
.. code-block :: pytest
$ pytest -v -m basic
2019-01-06 03:19:40 +08:00
=========================== test session starts ============================
2024-01-02 16:58:20 +08:00
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y -- $PYTHON_PREFIX/bin/python
2021-10-04 14:56:26 +08:00
cachedir: .pytest_cache
rootdir: /home/sweet/project
2021-01-25 22:52:23 +08:00
collecting ... collected 24 items / 21 deselected / 3 selected
2017-11-04 02:37:18 +08:00
2019-01-06 03:19:40 +08:00
test_pytest_param_example.py::test_eval[1+7-8] PASSED [ 33%]
test_pytest_param_example.py::test_eval[basic_2+4] PASSED [ 66%]
2019-01-31 00:25:38 +08:00
test_pytest_param_example.py::test_eval[basic_6*9] XFAIL [100%]
2017-11-04 02:37:18 +08:00
2021-01-25 22:52:23 +08:00
=============== 2 passed, 21 deselected, 1 xfailed in 0.12s ================
2017-11-04 02:37:18 +08:00
As the result:
2012-12-20 22:57:07 +08:00
2017-11-04 02:37:18 +08:00
- Four tests were collected
- One test was deselected because it doesn't have the `` basic `` mark.
- Three tests with the `` basic `` mark was selected.
- The test `` test_eval[1+7-8] `` passed, but the name is autogenerated and confusing.
- The test `` test_eval[basic_2+4] `` passed.
- The test `` test_eval[basic_6*9] `` was expected to fail and did fail.
2019-01-25 06:29:47 +08:00
2019-01-26 05:14:15 +08:00
.. _`parametrizing_conditional_raising`:
Parametrizing conditional raising
2019-01-25 06:29:47 +08:00
--------------------------------------------------------------------
2019-01-28 00:10:11 +08:00
Use :func: `pytest.raises` with the
:ref: `pytest.mark.parametrize ref` decorator to write parametrized tests
in which some tests raise exceptions and others do not.
2019-01-25 06:29:47 +08:00
2023-08-04 00:31:17 +08:00
`` contextlib.nullcontext `` can be used to test cases that are not expected to
raise exceptions but that should result in some value. The value is given as the
`` enter_result `` parameter, which will be available as the `` with `` statement’ s
target (`` e `` in the example below).
2022-06-30 01:13:15 +08:00
For example:
2019-01-28 00:10:11 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: python
2023-08-04 00:31:17 +08:00
from contextlib import nullcontext
2022-04-28 22:30:16 +08:00
2019-01-25 06:29:47 +08:00
import pytest
2019-08-07 04:34:58 +08:00
2019-01-25 06:29:47 +08:00
@pytest.mark.parametrize(
"example_input,expectation",
[
2023-08-04 00:31:17 +08:00
(3, nullcontext(2)),
(2, nullcontext(3)),
(1, nullcontext(6)),
2019-01-25 06:29:47 +08:00
(0, pytest.raises(ZeroDivisionError)),
],
)
def test_division(example_input, expectation):
"""Test how much I know division."""
2023-08-04 00:31:17 +08:00
with expectation as e:
assert (6 / example_input) == e
2019-01-25 06:29:47 +08:00
2023-08-04 00:31:17 +08:00
In the example above, the first three test cases should run without any
exceptions, while the fourth should raise a`` ZeroDivisionError `` exception,
which is expected by pytest.