How-To Guide

How to Structure Tests with Arrange-Act-Assert

Structure each test in three phases: arrange the input, act once, and assert the outcome, so the test reads as one behavior.

On this page

  1. Introduction
  2. Arrange the input in one phase
  3. Act once, on the behavior under test
  4. Assert the outcome, focused
  5. Keep one behavior per test and name it
  6. Test the structure by reading the failure
  7. Common pitfalls
  8. Conclusion

Introduction

A test that reads as three phases — arrange the input, act on it, assert the outcome — is easy to understand and to fix. The structure is not bureaucracy; it is what makes a test a statement of one behavior. When the phases are mixed, the test acts twice, asserts in between, and a failure does not say what broke. Arrange-Act-Assert, or Given-When-Then, is the shape that keeps a test focused.

This tutorial covers arranging the input with builders, acting once, asserting the outcome, and keeping one behavior per test. The examples are language-agnostic, and the same structure appears as Given-When-Then in behavior-driven testing.

Arrange the input in one phase

The arrange phase sets up everything the act needs: the input data, the collaborators, and their configured behavior. Use a test data builder so the arrange is short and the values that matter stand out. An arrange that fills ten lines with values the test does not care about buries the one value it does, and the reader has to find it.

Keep the arrange specific to the test in the test itself, and put only the genuinely shared setup in a hook. A reader should see the arrangement in the test, not hunt for it in a before hook. A little repetition across tests is worth the local clarity, because the test then reads as a self-contained statement.

test('rejects an expired token', () => {
  // arrange: the one relevant value, everything else default
  const token = aToken({ expiresAt: new Date('2020-01-01T00:00:00Z') });
  const clock = aClock({ now: new Date('2026-01-01T00:00:00Z') });

  // act: one call
  const result = verifyToken(token, clock);

  // assert: the outcome
  expect(result.valid).toBe(false);
  expect(result.reason).toBe('expired');
});

Act once, on the behavior under test

The act phase performs the single action the test is about, once. Two acts in one test check two behaviors, and a failure does not say which one broke; they also couple, because the first act’s state affects the second. If a test needs two acts, it is usually two tests, or the second act belongs in a separate test that arranges its own state.

The act should be a call to the code under test, not a setup step. If the test calls a helper to prepare the state, that is arrange; the act is the operation whose behavior the test verifies. Keeping the act to one line makes the test’s subject obvious and the failure’s location immediate.

Assert the outcome, focused

The assert phase states the expected result: the returned value, the thrown error, or the observable side effect. Assert what the behavior should produce, and keep the assertions on the behavior under test rather than on unrelated state. A test that asserts five things in the act’s aftermath tests five behaviors and is really several tests.

Prefer a specific assertion to a vague one. Assert the exact status and the error code, not merely that an error occurred; assert the total and the item count, not merely that the result is defined. A specific assertion catches a bug a vague one passes, and it documents the exact behavior the test guarantees.

// vague: passes for many wrong results
expect(result).toBeDefined();
// specific: names the exact behavior
expect(result.status).toBe(422);
expect(result.body.issues[0].path).toBe('items');

Keep one behavior per test and name it

The test name should state the behavior it checks, in the form of a rule such as ‘rejects an expired token’. A name that states the behavior makes a failure readable in the report: the failing name says which behavior broke, without opening the test. A name like ‘test1’ or ‘works’ tells nothing.

One behavior per test means one act and a few assertions about that act. A test that checks several behaviors should be split, so each failure names one behavior. This also makes the suite’s failures more informative: ten failures name ten behaviors, rather than one failure at the end of a long test that checked everything.

Test the structure by reading the failure

The structure pays off in the failure report. Assert that the test names are behaviors, so a report reads as a list of the behaviors that broke. Assert that a test with one act fails at the assert phase for the expected reason, by mutating the assertion and confirming the failure points at the behavior, not at a setup step.

# the failure report should read as behaviors, not as test numbers
npx vitest run --reporter=verbose
# e.g. "rejects an expired token" names the behavior that broke

Common pitfalls

Mixing the phases so the act is buried in setup, acting twice in one test so a failure is ambiguous, asserting unrelated state so the test checks several behaviors, a vague assertion that passes for wrong results, and a shared setup that hides what the test depends on are the recurring mistakes. Three clear phases, one act, focused specific assertions, one behavior per named test, and local arrange address them.

Conclusion

Arrange-Act-Assert makes a test a statement of one behavior. Arrange the input with builders in one phase, act once, assert the outcome with specific assertions, and keep one behavior per test with a name that states it. Self-check by reading the failure report and confirming each failure names a behavior. Then a red test says what broke, not that something in a long script failed.

Sources: Martin Fowler: Given-When-Then, Arrange-Act-Assert (c2 wiki), xUnit Patterns: Four-phase test.

FAQ

Why one act per test?
A test with two acts checks two behaviors, and a failure does not say which. It also couples the two: the first act's state affects the second, so the test is harder to read and to fix. One act keeps the test focused and its failure unambiguous.
Where do shared setup and teardown go?
Truly shared setup, such as starting a server, goes in a before hook. Setup specific to one test stays in its arrange phase, so a reader sees it in the test rather than hunting for it. Prefer a little repetition over a hidden setup that the test depends on.

Related Examples