Arrange-Act-Assert

A three-part structure for a test — set up the situation, perform the one action being tested, then check the result.

Purpose

Arrange-Act-Assert (AAA) splits a test into three visible parts: Arrange the inputs and any setup a test needs, Act by calling the one thing actually being tested, then Assert that the result is correct. It’s a convention, not something pytest enforces — but a test that follows it is far easier to read at a glance than one that interleaves setup, action, and checking.

Example

def test_celsius_to_fahrenheit_freezing():
    # Arrange
    celsius = 0

    # Act
    result = celsius_to_fahrenheit(celsius)

    # Assert
    assert result == 32

Behavior notes

  • Each test should have exactly one Act step — one call to the thing being tested. Multiple, unrelated actions in one test make it unclear which one actually failed when the test breaks, and usually mean the test should be split in two.
  • The comments (# Arrange, # Act, # Assert) are optional and often omitted once the shape becomes familiar — test_celsius_to_fahrenheit_boiling a line below this example, assert celsius_to_fahrenheit(100) == 212, is a one-line test that’s still recognizably AAA: arrange and act are combined into the function call’s argument, assert is the comparison.
  • “Assert” here means the test’s actual check (a plain Python assert statement, or pytest.raises(...) for an expected exception) — not Python’s assert keyword specifically, even though pytest test functions do usually use it directly.

Common mistakes

  • Testing several unrelated behaviors in one test function — when it fails, it’s unclear which behavior actually broke without reading the whole test body carefully.
  • Asserting on incidental details alongside the thing actually being tested (e.g. asserting on a log message’s exact wording in a test that’s really about a return value) — this makes tests brittle to unrelated changes.

Implementation notes

Arrange-Act-Assert is a general testing convention, not specific to Python or to any particular testing library — it applies identically whether the test framework is pytest, unittest, or something else entirely.

Taught in these lessons

Related reference

Updated 2026-09-04