Add examples of parametrizing classes and all tests in a module

Closes #8947

PR #8962
This commit is contained in:
Emmanuel Arias 2021-07-31 10:26:25 -03:00 committed by GitHub
parent 6247a95601
commit 2834b39b65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 1 deletions

View File

@ -36,6 +36,7 @@ Anton Grinevich
Anton Lodder
Antony Lee
Arel Cordero
Arias Emmanuel
Ariel Pillemer
Armin Rigo
Aron Coyle

View File

@ -110,7 +110,41 @@ 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.
(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,
for example with the builtin ``mark.xfail``: