parametrize

raw JSON →
0.2.0 verified Fri May 01 auth: no python

A drop-in replacement for @pytest.mark.parametrize that works with unittest.TestCase classes, enabling parameterized tests without needing pytest. Current version 0.2.0. Development appears low-activity.

pip install parametrize
error AttributeError: module 'parametrize' has no attribute 'parametrize'
cause Import using import parametrize instead of from parametrize import parametrize.
fix
from parametrize import parametrize
error TypeError: test_add() missing 2 required positional arguments: 'b' and 'expected'
cause Argument names in the decorator string do not match method parameters, or the tuple lengths mismatch.
fix
Ensure the argument names string (e.g., 'a,b,expected') matches method parameters exactly.
error ValueError: not enough values to unpack (expected x, got y)
cause Number of values in a test case tuple does not match the number of argument names.
fix
Each test tuple must have exactly as many elements as argument names.
deprecated parametrize is not actively maintained; consider using pytest's built-in parametrize directly if you can migrate tests away from unittest.TestCase.
fix Use @pytest.mark.parametrize and run tests with pytest instead of unittest.
gotcha The decorator must be placed above the method definition, not below. Placing it below will silently not work.
fix Ensure @parametrize is directly above the test method, with no other decorator in between.
gotcha Argument names in the string must exactly match method parameters. A mismatch will cause a TypeError at test execution.
fix Check that the comma-separated argument names match the method signature.

Basic usage with unittest.TestCase showing parameterized tests.

import unittest
from parametrize import parametrize

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

if __name__ == '__main__':
    unittest.main()