Skip to content

Arrange Act Assert

The Arrange Act Assert (AAA) testing pattern is a structured approach to writing unit tests, commonly used in Test Driven Development (TDD) and other testing methodologies. It helps organize the test code into three distinct sections: Arrange, Act, and Assert.

The AAA Pattern

Arrange: In this section, you set up the preconditions and initial state required for the test. This may involve creating objects, setting up variables, or preparing the environment for the test case.

Act: This is where you perform the actual action or behavior that you want to test. This could be calling a method, invoking a function, or executing a specific piece of code.

Assert: In this section, you verify the outcome of the action or behavior to ensure that it meets your expectations. You use assertions to check if the result matches the expected value or state.

Example of AAA Pattern

import pytest
from my_module import Calculator

@pytest.fixture
def calculator():
    return Calculator()

def test_addition(calculator):
    # Arrange
    a = 5
    b = 10

    # Act
    result = calculator.add(a, b)

    # Assert
    assert result == 15
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CalculatorTest {

    private Calculator calculator;

    @BeforeEach
    public void setUp() {
        calculator = new Calculator();
    }

    @Test
    public void testAddition() {
        // Arrange
        int a = 5;
        int b = 10;

        // Act
        int result = calculator.add(a, b);

        // Assert
        assertEquals(15, result);
    }
}

In this example:

Arrange: Initialize variables to be used in the calculator testing.

Act: Perform addition using the add method of the calculator instance.

Assert: Confirm that the result of the addition is equal to the expected value.