Registry / testing / parameterized

parameterized

JSON →
library0.9.0pypypi✓ verified 26d ago

Parameterized is a Python library that provides parameterized testing capabilities for various test frameworks like unittest, pytest, and nose. It simplifies writing data-driven tests by allowing the same test logic to be run with multiple sets of input data, reducing duplication and improving test coverage. The current version is 0.9.0, released in March 2023.

pip install parameterized
INSTALL
IMPORT
SIG · PARAMETERIZED
P
parameterized
testingpythonv0.9.0
Install
1.5s avg
Import
359ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.9.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.388s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.330s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parameterized
from parameterized import parameterized
from parametrized import parameterized
A common typo is to omit the 'e' from 'parameterized', leading to an ImportError or AttributeError.
param
from parameterized import param
Used for more explicit parameter definitions, especially with keyword arguments.
parameterized_class
from parameterized import parameterized_class
Used to parameterize an entire test class.

This quickstart demonstrates how to use `parameterized` with Python's built-in `unittest` framework. It shows examples for parameterizing individual test methods using `@parameterized` and `@parameterized.expand`, and how to parameterize an entire test class using `@parameterized_class`.

import unittest from parameterized import parameterized, param import math class TestMath(unittest.TestCase): @parameterized([ (2, 2, 4), (2, 3, 8), (1, 9, 1), (0, 9, 0), ]) def test_pow(self, base, exponent, expected): self.assertEqual(math.pow(base, exponent), expected) @parameterized.expand([ ("negative", -1.5, -2.0), ("integer", 1, 1.0), ("large fraction", 1.6, 1), ]) def test_floor(self, name, input_val, expected): self.assertEqual(math.floor(input_val), expected) @parameterized_class(('a', 'b', 'expected_sum'), [ (1, 2, 3), (5, 5, 10), ]) class TestMathClass(unittest.TestCase): def test_add(self): self.assertEqual(self.a + self.b, self.expected_sum) # To run these tests, you would typically use: # unittest.main(argv=['first-arg-is-ignored'], exit=False) # or a test runner like pytest/nose.
Debug
Known issues
breakingAs of version 0.9.0, `parameterized` has dropped support for Python 2.x, 3.5, and 3.6. If you require these Python versions, you must use an older version of `parameterized` (e.g., 0.8.1).
fix
Upgrade to Python 3.7+ or pin `parameterized` to version 0.8.1 or earlier.
affects: >=0.9.0
gotchaWhen combining `@parameterized` (or `@parameterized.expand`) with `@mock.patch`, the `@mock.patch` decorator must be placed *below* the `@parameterized` decorator. Additionally, the arguments introduced by `mock.patch` should appear *last* in the test method's signature. Incorrect ordering can lead to unexpected behavior or errors.
fix
Ensure `@mock.patch` is below `@parameterized` and mocked arguments are last in the function signature.
affects: All versions
gotchaIf you use an iterator or generator to supply parameters to `@parameterized` or `@parameterized.expand`, all items will be loaded into memory *before* the test run begins. This can be a significant memory concern for very large or infinite parameter sets.
fix
For very large datasets, consider generating test data on-the-fly within the test method itself, or exploring alternative parameterization methods suitable for streaming data if available within your test framework.
affects: All versions
gotchaA common pitfall is installing `parametrized` (missing the 'e') instead of `parameterized`. This will result in an `AttributeError: 'function' object has no attribute 'expand'` when trying to use `parameterized.expand`.
fix
Always ensure you install `parameterized` (with the 'e') via `pip install parameterized`.
affects: All versions
gotchaOvercomplicating parameterized test cases with excessive parameters or too many variations within a single test can lead to confusion, difficulty in maintenance, and unclear results. This defeats the purpose of parameterized testing.
fix
Keep test cases modular and focused. Break down complex scenarios into smaller, manageable tests. Each test should validate a specific aspect of functionality.
affects: All versions
breakingUsing `@parameterized` directly on a test method within a `unittest.TestCase` subclass will raise an exception. The library explicitly requires `@parameterized.expand` for parameterizing methods within `TestCase` subclasses.
fix
Replace `@parameterized` with `@parameterized.expand` when decorating test methods inside a `unittest.TestCase` subclass.
affects: All versions
breakingUsing the `@parameterized` decorator directly on a `unittest.TestCase` subclass will raise an `Exception`. The `@parameterized` decorator is designed for non-TestCase functions or classes. For parameterizing individual test methods within a `unittest.TestCase` subclass, you must use `@parameterized.expand`.
fix
When parameterizing tests within a `unittest.TestCase` subclass, apply `@parameterized.expand` to the specific test method, rather than using `@parameterized` on the class itself.
affects: All versions
Errors
Common errors & fixes
AttributeError: 'function' object has no attribute 'expand'
This error typically occurs when the incorrect package `parametrized` (missing the 'e') is installed instead of the correct `parameterized` library.
fix
Uninstall the wrong package (`pip uninstall parametrized`) and install the correct one (`pip install parameterized`).
ImportError: cannot import name 'parameterized_class'
This happens when `parameterized_class` is not directly exposed for import from the top-level `parameterized` package, or due to an older version of the library not exposing it directly.
fix
Ensure you are using a recent version of the library (0.9.0 or later) and import it as `from parameterized import parameterized_class`. If the issue persists, explicitly import from the submodule `from parameterized.parameterized import parameterized_class` if the library structure changed or you are on an older version.
ModuleNotFoundError: No module named 'parameterized'
The `parameterized` library is not installed in the Python environment being used.
fix
Install the library using pip: `pip install parameterized`.
UnboundLocalError: local variable 'patching' referenced before assignment
This error often arises when combining `@parameterized.expand` with `unittest.mock.patch` decorators and their order is incorrect, as the `patch` decorator must come *below* `parameterized.expand` to function properly.
fix
Ensure that the `@mock.patch(...)` decorator is placed *below* the `@parameterized.expand(...)` decorator. Also, the mocked parameters must come last in the test method's signature.
Upgrade
Version history
0.9.0latest on PyPI · released Mar 27, 2023
Audit
Dependencies
pythonrequiredRequires Python 3.7 or newer.
Agent activity
24 hits · last 30 days
node
22
Resources
parameterized — pip install parameterized · libregistry