def test_add_commutative(a, b): assert a b == b a
Testing Patterns

Meaning

Asserts that addition is commutative for the given operands.

Primary Function

Test the commutative property of addition in a unit test.

Communicative Purpose

Assert a mathematical property to verify correctness of an implementation.

Pattern

def test_<name>(<parameter_list>): assert <expression>

Core Structure

def test_<name>(<params>): assert <expr> where expr uses the params.

Função primária

Test the commutative property of addition in a unit test.

Propósito comunicativo

Assert a mathematical property to verify correctness of an implementation.

Situações de gatilho

When writing unit tests for numeric types or custom types that implement addition.

Contextos

Unit testing suites, property-based testing, educational examples of algebraic properties.

Padrão

def test_<name>(<parameter_list>): assert <expression>

Estrutura central

def test_<name>(<params>): assert <expr> where expr uses the params.

Slots de substituição

<name>, <parameter_list>, <expression>

Colocados típicos

  • def
  • test_
  • assert
  • ==
  • +

Substituições comuns

  • Different operators (*
  • -)
  • different variable names
  • using self.assertEqual in unittest frameworks.

Erros comuns

Using assignment (=) instead of equality (==), forgetting self. in unittest methods, omitting parentheses.

Similar / contraste

Tests for associativity ((a + b) + c == a + (b + c)), distributivity (a * (b + c) == a*b + a*c).

Interferências

Confusing assignment with equality; confusing commutativity with other algebraic properties.

Família do chunk

  • unit-test assertion patterns

Nuance

Assumes the operands support the + operator and that equality is meaningful for those types.

Efeito pragmático

Serves as an executable specification of the commutative property.

Dica de memória

commutes

Nota

Assumes the operands support the + operator and that equality is meaningful; for custom types ensure __eq__ is defined.

Upgrade path

Move to property‑based testing libraries (e.g., Hypothesis) for generic property testing.

Tipo de construção: Function definition with an assert statement.Tag de espaçamento: Short-term

Log in to save chunks.