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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: