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

Meaning

Defines a parametrized test case in pytest, providing input-output pairs for a test function.

Primary Function

Generates multiple test invocations from a single test function using supplied argument tuples.

Communicative Purpose

Instructs the test runner to execute the decorated test function once per set of argument values.

Pattern

@pytest.mark.parametrize('<arg_names>', [<tuple_of_values>])

Core Structure

@pytest.mark.parametrize followed by parentheses containing a comma‑separated string of argument names and a list of tuples holding the test values.

Função primária

Generates multiple test invocations from a single test function using supplied argument tuples.

Propósito comunicativo

Instructs the test runner to execute the decorated test function once per set of argument values.

Situações de gatilho

When writing repetitive test cases that differ only in input values, to reduce boilerplate and improve readability.

Contextos

Inside a pytest test module, directly above a test function definition that accepts the parametrized arguments.

Padrão

@pytest.mark.parametrize('<arg_names>', [<tuple_of_values>])

Estrutura central

@pytest.mark.parametrize followed by parentheses containing a comma‑separated string of argument names and a list of tuples holding the test values.

Slots de substituição

arg_names (comma‑separated string of parameter names), test_cases (list of tuples matching the number of arguments)

Colocados típicos

  • pytest
  • test
  • def test_
  • assert
  • param
  • ids

Substituições comuns

  • arg names: 'x
  • expected'
  • 'input
  • expected'
  • test cases: [(1
  • 2)
  • (2
  • 4)]
  • [(0
  • 0)
  • (1
  • 1)
  • (2
  • 4)]
  • [('hello'
  • 5)
  • ('world'
  • 5)]

Erros comuns

Mismatch between number of argument names and tuple elements, forgetting to import pytest.mark, using incorrect argument names in the test function, assuming lists are required (tuples work but lists are also accepted), mismatched data types.

Similar / contraste

Similar to @pytest.mark.parametrize(indirect=True) for fixture parametrization; contrasted with @pytest.fixture for providing setup data and with @pytest.mark.parametrize without ids for anonymous test cases.

Interferências

Confusing @pytest.mark.parametrize with @pytest.mark.parametrize(indirect=...) when using fixtures; mixing up argument order; forgetting that each tuple corresponds to one test invocation.

Família do chunk

  • pytest parametrize pattern

Nuance

Enables data‑driven testing while keeping test code concise; each tuple yields a distinct test case reported separately by pytest.

Efeito pragmático

Signals to readers that the test is data‑driven, encouraging concise and maintainable test suites.

Dica de memória

Parametrize your test with input‑output pairs to avoid repetitive test functions.

Nota

The decorator generates separate test invocations; each tuple produces its own test case visible in pytest output.

Upgrade path

Consider adding ids for better readability, or switch to indirect parametrization with fixtures for more complex setups.

Tipo de construção: decorator applicationTag de espaçamento: Short-term

Log in to save chunks.