def test_function_name(value, expected): assert function_under_test(value) is expected
Testing Patterns

Meaning

A parametrized test function that verifies a function's return value matches an expected result using identity comparison. Addresses the need to systematically validate function behavior against known inputs and outputs. Used when writing unit tests for functions that return singleton values like booleans or None.

Primary Function

Unit testing

Communicative Purpose

Ensures function outputs are verified against expected values through identity comparison in a reusable test structure

Pattern

def test_function_name(value, expected): assert function_under_test(value) is expected

Core Structure

def test_...(..., expected): assert ...(...) is expected

Função primária

Unit testing

Propósito comunicativo

Ensures function outputs are verified against expected values through identity comparison in a reusable test structure

Situações de gatilho

Unit testing: verifying boolean-returning functions produce correct truth values Test-driven development: defining expected behavior before implementation Regression testing: guarding against changes in predicate function output

Contextos

pytest, unittest, test suites, CI/CD pipelines

Padrão

def test_function_name(value, expected): assert function_under_test(value) is expected

Estrutura central

def test_...(..., expected): assert ...(...) is expected

Slots de substituição

test_function_name: valid identifier starting with test_, value: any test input, expected: expected return value (singleton), function_under_test: function being tested

Colocados típicos

  • pytest.mark.parametrize
  • pytest.raises
  • unittest.TestCase
  • assert statements

Substituições comuns

  • input can be any value
  • expected can be True or False
  • function name can be any predicate
  • test name can describe the scenario

Erros comuns

Using input as parameter name: shadows Python built-in input() function → confusing NameError or silent bugs when built-in is needed later Using is instead of == for non-singleton values: identity check fails for strings or large integers → test passes for small ints due to interning but fails unpredictably for larger values Forgetting test_ prefix: pytest only discovers functions starting with test_ → test silently skipped without warning

Similar / contraste

assertEqual (unittest): uses == equality rather than is identity pytest.approx: for approximate floating-point comparison assert ... == ...: value equality check suitable for all types

Interferências

Coming from Java: may write assertEquals(expected, actual) with reversed argument order → Python assert uses natural left-to-right reading order Coming from JavaScript: may use === for strict equality → Python's is checks object identity not strict value equality

Família do chunk

  • assert statement
  • pytest.mark.parametrize
  • unittest.TestCase
  • test fixtures

Nuance

Do not use is for comparing non-singleton values like strings or large integers as identity is implementation-dependent. is comparison works reliably only for None, True, False, and small integers (-5 to 256) due to CPython interning. pytest rewrites assert statements to provide detailed failure messages but bare assert without pytest gives minimal output.

Efeito pragmático

Catches boolean logic regressions early and provides clear test intent when verifying truth-value functions

Dica de memória

Like a quality inspector stamping parts pass or fail — the test function checks each input against its expected identity badge

Nota

The is operator in assertions is appropriate only for singleton comparisons (None, True, False); for general value comparison use == instead

Upgrade path

pytest.mark.parametrize for data-driven testing with multiple input-output pairs in a single test

Frequência: HighFormulaicidade: Semi-fixedTipo de construção: Function definition containing an assert statement.Prioridade de aquisição: Automatic productionPrioridade de output: BothTag de espaçamento: Short-term

Log in to save chunks.