What is the Arrange-Act-Assert (AAA) pattern?
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
Assertcalls 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_ThrowsorGiven_EmptyCart_When_Checkout_Then_ReturnsError.