@pytest.mark.parametrize('input,expected'
Testing Patterns

Meaning

Parametrizes a pytest test function with multiple sets of input arguments and expected results.

Primary Function

Enables data-driven testing by generating multiple test invocations from a single test function.

Communicative Purpose

Indicates that the decorated test function should be executed once for each tuple in the provided list, binding the tuple elements to the parameter names.

Pattern

@pytest.mark.parametrize('param_names', [(arg1, expected1), (arg2, expected2), ...])

Core Structure

@pytest.mark.parametrize(string_of_param_names, list_of_tuples)

Função primária

Enables data-driven testing by generating multiple test invocations from a single test function.

Propósito comunicativo

Indicates that the decorated test function should be executed once for each tuple in the provided list, binding the tuple elements to the parameter names.

Situações de gatilho

When writing pytest test functions that need to be run with several different input datasets to avoid duplicating test logic.

Contextos

Inside pytest test modules, placed directly above a test function definition.

Padrão

@pytest.mark.parametrize('param_names', [(arg1, expected1), (arg2, expected2), ...])

Estrutura central

@pytest.mark.parametrize(string_of_param_names, list_of_tuples)

Slots de substituição

param_names (string of comma-separated argument names), list_of_tuples (each tuple length matches number of param_names)

Colocados típicos

  • pytest
  • test
  • fixture
  • mark.parametrize
  • test_function

Substituições comuns

  • param names string
  • list of tuples or lists
  • use of ids parameter
  • indirect fixture usage

Erros comuns

Mismatched number of values per tuple, mismatched argument count, missing commas, incorrect data types, forgetting the * unpacking when needed

Similar / contraste

@pytest.mark.parametrize with indirect, @pytest.mark.parametrize with ids, @pytest.fixture(params=...)

Interferências

Confusing the order of values in tuples, mixing up argument names with fixture names, misunderstanding positional vs keyword binding

Família do chunk

  • pytest parametrize decorator

Nuance

Enables clear, data-driven test cases; ids improve readability; indirect mode allows fixture-driven setup

Efeito pragmático

Signals that the test is data-driven, reducing boilerplate and making test intent explicit

Dica de memória

@pytest.mark.parametrize

Nota

Consider adding descriptive ids via the ids argument to make test output more readable

Upgrade path

Add ids for clarity, or migrate to indirect fixtures for more complex setup/teardown

Frequência: HighFormulaicidade: Semi-fixedTipo de construção: decorator applicationPrioridade de aquisição: Active recallPrioridade de output: OutputTag de espaçamento: Short-term

Log in to save chunks.