PHP › Unit Testing with PHPUnit

What is the arrange act assert pattern, and how does structuring a test around these three distinct steps make tests clearer and easier to understand?

Intermediate

Answer

The arrange act assert pattern is a widely recommended way to structure the internal logic of an individual test, first arranging any necessary setup, such as creating the objects and data the test will need, then performing the single specific action being tested, such as calling the particular method under test, and finally asserting that the actual resulting outcome matches what was genuinely expected, and consistently following this three step structure across every test makes each individual test easier to read and understand at a glance, even for someone unfamiliar with that specific piece of code.
public function testDiscountIsAppliedCorrectly(): void
{
    // Arrange
    $cart = new ShoppingCart();
    $cart->addItem('Book', 20.00);

    // Act
    $cart->applyDiscount(0.10);

    // Assert
    $this->assertEquals(18.00, $cart->getTotal());
}
Real-world example A test verifying a shopping cart's discount logic clearly separates the setup of adding an item, the actual action of applying a ten percent discount, and the final assertion checking the resulting total, making the test's intent immediately obvious to anyone reading it.

Common follow-ups: Does every single test always need to strictly follow this exact three step structure?;How does this pattern relate to keeping individual tests focused on testing just one specific behavior?