What is the Arrange-Act-Assert (AAA) pattern?

Unit Testing & TDD Fundamentals Basic Fresher1–2 Yrs Conceptual 1 min read

Quick Answer

AAA structures a test into three clear sections: Arrange — set up the object under test and its inputs; Act — call the one method being tested; Assert — verify the outcome. It keeps tests readable and makes it obvious what's being tested. “Given-When-Then” is the same idea in BDD language.

Every test I write has three parts. Arrange sets up the thing under test and whatever inputs and fakes it needs. Act is the single call to the method I'm actually testing. Assert checks the result. Keeping them visually separate makes the test easy to read and makes it obvious when a test is doing too much — if the Act section is two calls, that's a smell. It's the same thing as Given-When-Then in BDD.

  • One logical assertion per test — you can have multiple Assert calls that check one behaviour, but a test shouldn't verify two unrelated things.
  • If “Act” is more than one line, or you have two, the test is probably doing too much.
  • Good test names describe the scenario and expected result: Withdraw_WithInsufficientFunds_Throws or Given_EmptyCart_When_Checkout_Then_ReturnsError.