Arrange-Act-Assert - testing pattern


Python clean test tip:

Structure your tests in an Arrange-Act-Assert way:

  • Arrange - set-up logic
  • Act - invokes the system you're about to test
  • Assert - verifies that the action of the system under test behaves as expected

Example:

from dataclasses import dataclass


@dataclass
class User:
    first_name: str
    last_name: str

    def full_name(self):
        return f"{self.first_name} {self.last_name}"


def test_full_name_consists_of_first_name_and_last_name():
    # arrange
    first_name = "John"
    last_name = "Doe"
    user = User(first_name=first_name, last_name=last_name)

    # act
    full_name = user.full_name()

    # assert
    assert full_name == "John Doe"