Meaning
The parameterized.expand decorator generates multiple test methods from a single test function, each with a different set of arguments taken from a list of tuples. It enables data‑driven (table‑driven) unit tests without writing repetitive test code. Use it when you need to verify the same logic across many input/output pairs.
Primary Function
Testing / Test generation
Communicative Purpose
Reduces duplication in unit tests by expressing test cases as data.
Pattern
@parameterized.expand([; ]) def test_; (self, ; ): self.assertEqual(; (; ), ; )
Core Structure
@parameterized.expand([; ]) def test_; (self, ; ): self.assertEqual(; (; ), ; )
Função primária
Testing / Test generation
Propósito comunicativo
Reduces duplication in unit tests by expressing test cases as data.
Situações de gatilho
Testing a pure function with multiple input‑output examples; verifying edge cases for a mathematical operation; validating API responses for varied request payloads.
Contextos
Python unit testing with unittest and the parameterized library; also appears in test suites for libraries, frameworks, and CI pipelines.
Padrão
@parameterized.expand([; ]) def test_; (self, ; ): self.assertEqual(; (; ), ; )
Estrutura central
@parameterized.expand([; ]) def test_; (self, ; ): self.assertEqual(; (; ), ; )
Slots de substituição
test_data: list of (input..., expected) tuples; test_name: descriptive test method name; params: parameter names matching function arguments; func_call: function call expression; expected_var: name of expected result variable
Colocados típicos
- unittest.TestCase
- parameterized.parameterized
- assertEqual
- test fixtures
Substituições comuns
- using @parameterized.product for Cartesian product
- using unittest.subTest loop
- using pytest.mark.parametrize
Erros comuns
forgetting to import parameterized; mismatched tuple length vs function parameters; using mutable default data that changes across tests; not providing descriptive test names leading to unclear test output.
Similar / contraste
pytest.mark.parametrize (native pytest decorator, more concise); nose's @with for data-driven tests; manual loop with subTest.
Interferências
Coming from JUnit: expecting @Parameters annotation with a static method; syntax differs.
Família do chunk
- data-driven testing
- table-driven tests
- parametrized testing
Nuance
Each generated test runs independently, so failures pinpoint the exact data case; however, large data sets can slow test runs and produce verbose output; avoid using it for tests that require shared setup that cannot be reset per case.
Efeito pragmático
Makes test intent explicit and eliminates boilerplate, simplifying maintenance when adding new test cases.
Dica de memória
Think of a spreadsheet: each row becomes a test.
Nota
Requires the `parameterized` package (`pip install parameterized`). Each generated test runs independently, making failures point to the exact data case.
Upgrade path
Switch to pytest.mark.parametrize for native pytest support.
Log in to save chunks.