Introduction to the Arrange-Act-Assert Pattern

Learn how to structure end-to-end tests using the Arrange-Act-Assert pattern.

When writing end-to-end tests, we often test complex scenarios with multiple services running simultaneously. We need a systematic way to write tests that are simple to understand. While Cypress makes writing our tests simple by providing us with some basic building blocks, it is fairly uncomplicated when it comes to structuring our tests. Having a reproducible structure makes writing even complicated test scenarios straightforward. Let’s look at one of the most common formulas, the Arrange-Act-Assert pattern.

Testing formula

Most tests can be divided into three distinct steps: arrange, act, and assert. When writing our tests, it’s helpful to take some time and try to follow this pattern. Using the same structure for all of our tests makes it easy to understand what is happening in each test with a quick glance. Let’s learn what we should do in each step.

Arrange

In the first step, we set up our application state to our liking. Here are some actions that we perform:

  • Resetting the database

  • Seeding the database

  • Setting up cookies

  • Visiting the correct URL

Act

In the second step, we perform actions that cause some event to happen, such as:

  • Filling up form fields

  • Clicking a button

  • Clicking on a link

Assert

In the last step, we check that our application state matches what we expect it to.

Get hands-on with 1200+ tech skills courses.