self.assertEqual
Testing Patterns

Meaning

Asserts that two values are equal using unittest's assertEqual method.

Primary Function

Verifies equality of two expressions in a unit test, raising an AssertionError if they differ.

Communicative Purpose

Communicates the tester's expectation that the actual result equals the expected result, signalling test success or failure.

Pattern

self.assertEqual(arg1, arg2)

Core Structure

self.assertEqual(first, second) where first and second are expressions.

Função primária

Verifies equality of two expressions in a unit test, raising an AssertionError if they differ.

Propósito comunicativo

Communicates the tester's expectation that the actual result equals the expected result, signalling test success or failure.

Situações de gatilho

When writing a unit test to confirm that a function or expression produces an expected value.

Contextos

Inside a method of a subclass of unittest.TestCase, typically after exercising the code under test.

Padrão

self.assertEqual(arg1, arg2)

Estrutura central

self.assertEqual(first, second) where first and second are expressions.

Slots de substituição

first: expression to test, second: expected value expression

Colocados típicos

  • import unittest
  • class TestXXX(unittest.TestCase)
  • def test_*:
  • self.assertTrue
  • self.assertFalse
  • self.assertRaises

Substituições comuns

  • Using pytest's plain assert: assert actual == expected
  • using unittest's assertEqual with msg parameter: self.assertEqual(a
  • b
  • 'message')
  • using numpy's assert_array_equal for arrays.

Erros comuns

{"cause":"Reversing expected and actual arguments","consequence":"Test fails on correct output because assertion message is confusing"} {"cause":"Using assertEqual on floating‑point values without rounding","consequence":"False failures due to floating‑point rounding error"} {"cause":"Forgot to import unittest or inherit from TestCase","consequence":"AttributeError: 'TestCase' object has no attribute 'assertEqual' at runtime"} {"cause":"Using assertEqual with mutable objects that change after the call","consequence":"Unreliable test outcome"} {"cause":"Providing a non‑hashable objects where equality is expensive","consequence":"Slow test execution"}

Similar / contraste

{"concept":"self.assertNotEqual","distinction":"Asserts that two values are NOT equal"} {"concept":"self.assertTrue","distinction":"Asserts a condition is True, less specific than equality"} {"concept":"self.assertFalse","distinction":"Asserts a condition is False"} {"concept":"pytest's plain assert","distinction":"Uses Python's assert statement; provides less detailed failure info unless using pytest‑assertions"}

Interferências

{"source":"Java's JUnit","consequence":"May incorrectly assume argument order is (expected, actual) like assertEquals(expected, actual); in unittest it's (actual, expected)"} {"source":"Rust's assert_eq!","consequence":"May expect macro‑style syntax and forget the self. prefix and parentheses"} {"source":"JavaScript's assert.deepStrictEqual","consequence":"May expect deep equality by default; unittest's assertEqual uses ==, not deep equality for containers"}

Família do chunk

  • self.assertTrue
  • self.assertFalse
  • self.assertRaises
  • self.assertIsNone

Nuance

Avoid using assertEqual for complex data structures where deep equality is needed; prefer assertEqual only when == semantics suffice. The assertion itself is O(1) for simple objects but can be expensive for large containers due to __eq__ implementation; consider specialized asserts for large data. When comparing floating‑point numbers, use assertAlmostEqual with a delta or places argument to avoid false failures due to representation error.

Efeito pragmático

Provides a clear, automated contract that code behaves as expected; catching regressions early and serving as executable documentation.

Dica de memória

Think of assertEqual as a friendly referee who blows the whistle only when the two sides don't match, keeping the game fair.

Nota

The method name is assertEqual (not assertEquals); the latter is a deprecated alias in older unittest versions.

Tipo de construção: method invocationTag de espaçamento: Immediate

Log in to save chunks.