unittest-parametrize

raw JSON →
1.8.0 verified Mon Apr 27 auth: no python

A library to parametrize tests within unittest TestCases using decorators. Current version 1.8.0, released 2024-11-15, with monthly or on-demand releases.

pip install unittest-parametrize
error ModuleNotFoundError: No module named 'unittest_parametrize'
cause Package not installed or misspelled import.
fix
Install with 'pip install unittest-parametrize' and import as 'from unittest_parametrize import parametrize'.
error TypeError: 'parametrize' object is not callable
cause Importing the wrong symbol, e.g., from unittest.parametrize import parametrize.
fix
Use correct import: 'from unittest_parametrize import parametrize'.
gotcha unittest-parametrize only works with unittest.TestCase subclasses, not with pytest or other frameworks. Cannot be used with pytest fixtures.
fix Use pytest.mark.parametrize if using pytest.
gotcha The decorator must be applied before any other decorators that modify test methods (e.g., @mock.patch). Order matters: parametrize should be the outermost decorator.
fix Place @parametrize directly above the method definition, with other decorators below it.
deprecated Support for Python 3.8 has been dropped since version 1.6.0.
fix Upgrade to Python 3.9+.
breaking In version 1.8.0, the 'ids' parameter changed from accepting a list of strings to also accepting a callable. Code that relied on the old exact list format may break if the callable returns different ordering.
fix If you use ids as a list, it still works. Only breaking if you passed a callable that was previously ignored; now it is used to generate ids.

Basic parametrization of a test method with multiple sets of arguments.

from unittest import TestCase
from unittest_parametrize import parametrize

class MyTest(TestCase):
    @parametrize(
        ('a', 'b', 'expected'),
        [
            (1, 2, 3),
            (4, 5, 9),
        ],
    )
    def test_add(self, a, b, expected):
        self.assertEqual(a + b, expected)