From 2834b39b65bd97893d35bd138a5071a4dfd52676 Mon Sep 17 00:00:00 2001 From: Emmanuel Arias Date: Sat, 31 Jul 2021 10:26:25 -0300 Subject: [PATCH] Add examples of parametrizing classes and all tests in a module Closes #8947 PR #8962 --- AUTHORS | 1 + doc/en/how-to/parametrize.rst | 36 ++++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index d6004f33d..f68a75095 100644 --- a/AUTHORS +++ b/AUTHORS @@ -36,6 +36,7 @@ Anton Grinevich Anton Lodder Antony Lee Arel Cordero +Arias Emmanuel Ariel Pillemer Armin Rigo Aron Coyle diff --git a/doc/en/how-to/parametrize.rst b/doc/en/how-to/parametrize.rst index 7f5f6ea0c..6c2bac4ea 100644 --- a/doc/en/how-to/parametrize.rst +++ b/doc/en/how-to/parametrize.rst @@ -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``: