From 520309e56715c58a772c0489b6fb5f69560f1190 Mon Sep 17 00:00:00 2001 From: Paul Price Date: Tue, 20 Jul 2021 11:24:23 -0400 Subject: [PATCH] add more python unit test decorators The classParameters and methodParameters decorators are useful, but sometimes you want to iterate over all combinations of some parameters, but don't want to go to the trouble of listing all combinations. This commit adds classParametersProduct and methodParmaetersProduct that generates the cartesian product of the provided parameters, and iterates over them. --- python/testing.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/python/testing.rst b/python/testing.rst index bb2e809d0..29e98893e 100644 --- a/python/testing.rst +++ b/python/testing.rst @@ -398,6 +398,27 @@ will run tests: Note that the method being decorated must be within a subclass of ``unittest.TestCase``, since it relies on the existence of the ``subTest`` method for identifying the individual iterations. This use of ``subTest`` also means that all iterations will be executed, not stopping at the first failure. +``classParameters`` and ``methodParameters`` require listing all combinations of the parameters. +If you want to instead iterate over all possible combinations of the parameters, +use ``classParametersProduct`` and ``methodParametersProduct``. +For example: + +.. code-block:: python + + class MyTestCase(unittest.TestCase): + @methodParametersProduct(foo=[1, 2], bar=["black", "white"]) + def testSomething(self, foo, bar): + ... + +will run tests: + +.. code-block:: python + + testSomething(foo=1, bar="black") + testSomething(foo=1, bar="white") + testSomething(foo=2, bar="black") + testSomething(foo=2, bar="white") + See also ``pytest.mark.parametrize``, which is more complex but offers similar functionality; however, this module is not yet approved for use.