Skip to content

Testing & QA

Arrange–Act–Assert

The idea, in plain English

Arrange–Act–Assert is a three-step shape that keeps every test easy to read. Think of a science experiment: first you set up the beakers (Arrange), then you mix the chemicals (Act), then you check what happened (Assert). Keeping those three steps in that order, with nothing tangled together, means anyone can glance at a test and instantly see the setup, the single action being tested, and the expectation.

How it works

  1. 1Arrange: build the world the test needs — create objects, set starting values, prepare inputs.
  2. 2Act: perform the one action you're actually testing. Just one, so a failure has a single obvious cause.
  3. 3Assert: check the result against what you expected. This is where PASS or FAIL is decided.
  4. 4Keep the three visually separate. A reader should see 'set up, do the thing, check the thing' without hunting.

When you'd use it

Use this shape for essentially every test you write. It's the default structure testing guides teach because it makes tests read like a short, honest story instead of a puzzle.

Common beginner mistakes

  • Doing several actions in the Act step. If a test both adds AND removes AND updates before asserting, a failure could come from any of them.
  • Sneaking assertions into the middle. Save the checking for the end so the 'what am I actually testing?' stays crystal clear.

Try it — edit and run

Click the code to edit · press ⌘/Ctrl+↵ to run

Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.

Expected output — hit Run to try it
Arrange: empty cart created
Act: added items 30 and 12
Assert: total is 42 (expected 42)
PASS

Not sure this is the right topic? See the learning paths → or where this leads →