Use the AAA pattern in tests.

Arrange, act, assert.

These are the key three components of good tests. First, one arranges a situation. This sets up the initial conditions of the test, modeling what the existing values of variables should be. In more complex tests, this can involve several factories or data providers. External dependencies may be mocked out, as those are typically tested elsewhere.

The second section is to act. Here, one performs the action that needs to be tested.

Finally in the third section, the test will assert that the final conditions are expected.

It is helpful to label these sections with “given”, “when”, and “then”. This pattern models the acceptance criteria of a story. In this way, one can use the acceptance criteria as a driver on how to drive the main goals of the test.

Leave a comment