Skip to content

Commit 2834b39

Browse files
authored
Add examples of parametrizing classes and all tests in a module
Closes #8947 PR #8962
1 parent 6247a95 commit 2834b39

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Anton Grinevich
3636
Anton Lodder
3737
Antony Lee
3838
Arel Cordero
39+
Arias Emmanuel
3940
Ariel Pillemer
4041
Armin Rigo
4142
Aron Coyle

doc/en/how-to/parametrize.rst

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,41 @@ the simple test function. And as usual with test function arguments,
110110
you can see the ``input`` and ``output`` values in the traceback.
111111

112112
Note that you could also use the parametrize marker on a class or a module
113-
(see :ref:`mark`) which would invoke several functions with the argument sets.
113+
(see :ref:`mark`) which would invoke several functions with the argument sets,
114+
for instance:
115+
116+
117+
.. code-block:: python
118+
119+
import pytest
120+
121+
122+
@pytest.mark.parametrize("n,expected", [(1, 2), (3, 4)])
123+
class TestClass:
124+
def test_simple_case(self, n, expected):
125+
assert n + 1 == expected
126+
127+
def test_weird_simple_case(self, n, expected):
128+
assert (n * 1) + 1 == expected
129+
130+
131+
To parametrize all tests in a module, you can assign to the :globalvar:`pytestmark` global variable:
132+
133+
134+
.. code-block:: python
135+
136+
import pytest
137+
138+
pytestmark = pytest.mark.parametrize("n,expected", [(1, 2), (3, 4)])
139+
140+
141+
class TestClass:
142+
def test_simple_case(self, n, expected):
143+
assert n + 1 == expected
144+
145+
def test_weird_simple_case(self, n, expected):
146+
assert (n * 1) + 1 == expected
147+
114148
115149
It is also possible to mark individual test instances within parametrize,
116150
for example with the builtin ``mark.xfail``:

0 commit comments

Comments
 (0)