@pytest.mark.parametrize('a,b,expected'
Testing Patterns

Meaning

Parametrizes a test function with multiple sets of arguments, causing the test to run once per argument tuple.

Primary Function

Generates multiple test invocations from a single test function by supplying argument tuples.

Communicative Purpose

Indicates that the decorated test function should be executed repeatedly with different input values.

Pattern

@pytest.mark.parametrize('arg1,arg2,...', [(val1,val2,...), ...])

Core Structure

@pytest.mark.parametrize(arg_names, arg_values) where arg_names is a comma‑separated string of parameter names and arg_values is an iterable of tuples matching those names.

Função primária

Generates multiple test invocations from a single test function by supplying argument tuples.

Propósito comunicativo

Indicates that the decorated test function should be executed repeatedly with different input values.

Situações de gatilho

When you need to test a function with several input-output pairs, especially for table‑driven or property‑based testing scenarios.

Contextos

Inside a Python test module, placed directly above a test function definition that uses pytest.

Padrão

@pytest.mark.parametrize('arg1,arg2,...', [(val1,val2,...), ...])

Estrutura central

@pytest.mark.parametrize(arg_names, arg_values) where arg_names is a comma‑separated string of parameter names and arg_values is an iterable of tuples matching those names.

Slots de substituição

arg_names: str of comma‑separated parameter names, arg_values: iterable of tuples with values matching the arg_names count

Colocados típicos

  • def test_...
  • assert statements
  • pytest.raises
  • fixtures
  • ids parameter

Substituições comuns

  • Using zip to combine separate lists
  • using the ids parameter for custom test IDs
  • using indirect to pass fixtures as arguments

Erros comuns

1. Mismatched number of values per tuple vs. arg names → ValueError during test collection.\n2. Using mutable default objects in param lists → shared state across tests.\n3. Forgetting the decorator → test runs once with missing arguments.\n4. Passing a list of lists instead of tuples (still works but less idiomatic).\n5. Using non‑hashable items in the ids parameter → errors.

Similar / contraste

pytest.mark.parametrize vs unittest.subTest: subTest runs iterations within a single test method, giving less isolated reporting; pytest.mark.parametrize with indirect: passes arguments as fixtures for dynamic setup; pytest.mark.parametrize with ids: customizes test IDs for clearer output.

Interferências

Coming from unittest: may write loops inside a test method instead of using parametrize → less isolated test reporting and harder failure pinning; use parametrize for separate test invocations.

Família do chunk

  • pytest.mark.parametrize
  • pytest.fixture
  • pytest.mark.parametrize with indirect
  • pytest.mark.parametrize with ids

Nuance

1. Avoid parametrizing tests that require heavy shared setup; use fixtures to avoid redundant work.\n2. Many parametrized cases increase test runtime linearly; consider sampling or property‑based testing for large domains.\n3. When using indirect parameters, the fixture must accept the argument as a parameter; otherwise the fixture receives the raw value, causing fixture misuse.

Dica de memória

Think of @pytest.mark.parametrize as a stamp that stamps out multiple copies of a test, each with its own set of arguments, like a cookie cutter shaping dough.

Nota

The ids parameter can accept a callable to generate IDs dynamically, improving readability of parametrized test outputs.

Upgrade path

Using parametrize with indirect fixtures to dynamically generate test data based on parameters.

Tipo de construção: decoratorTag de espaçamento: Medium-term

Log in to save chunks.