Understanding the AAA Framework: Arrange, Act, Assert

By

Table of Content

Arrange-Act-Assert (AAA) is a popular framework for testing. It provides a structured approach to organizing tests, making them easy to read and understand. Although it is widely used for writing unit tests, we believe this powerful framework can also be incredibly beneficial for higher-level tests such as User Interface testing. Let's explore the AAA framework's versatile applications, understanding its advantages, potential pitfalls, and how it might assist in your UI testing.

What is Arrange, Act, and Assert?

In the AAA framework, the three As segment the testing process, simplifying each test's structure and enhancing its readability. Each test has three parts: 

  • Arrange: Set the stage. Ensure everything is in place for the test to run.
  • Act: Execute the desired action you want to test.
  • Assert: Validate whether the result aligns with expectations, signaling either a pass or a need for corrections.

Let's consider an example of a sample test for the "Add to Cart" feature on an e-commerce website similar to Amazon:

Arrange (Setup)
  • Log into a test account
  • Search for the book, "Harry Potter and the Sorcerer's Stone"
  • Navigate to the book's details page where you see details like price and author
Act (Perform Action)
  • Click on the "Add to Cart" button for the book
Assert (Check)
  • Confirm if the book is in the cart with the correct price displayed
  • Confirm if the price of the book matches the price on the book details page
AAA Testing Framework
Sample test for "Add to Cart" feature using AAA framework

Key Benefits of Using the AAA Framework

By using the AAA framework, you get:

A roadmap for testing

You have a structured approach to your tests. You know where to begin, what to do next, and what to verify.

Quick identification of issues

You get clarity on what went wrong if something doesn't function properly. For instance, if a book isn't in the cart, maybe there is an issue with the "Add to Cart" button.

Detailed and accurate bug reporting

Reporting bugs or issues becomes easier and more accurate. You can provide detailed steps on how you tested, making the developers' troubleshooting journey smoother.

Extending AAA to UI Testing: What You Stand to Gain

The AAA framework is traditionally associated with unit testing due to its simplicity and straightforwardness. However, AAA's structured approach is now gaining popularity in higher-level tests, such as UI tests, for several reasons, such as:

Clarity and Structure

AAA provides a clear three-step process that segments the test setup, execution, and verification. Even for higher-level tests, this segmentation helps in understanding the test's intention and expected outcome. By adhering to the AAA structure, tests can remain consistent, readable, and easy to understand for all team members.

Focused Testing

The AAA framework encourages testers to ensure that each test has a singular, clear focus. It's beneficial if each test verifies one main thing, making it clear what is broken when a test fails. This in turn helps in:

  1. Quickly isolating bugs and communicating them to the development team.
  2. Aiding developers in reproducing issues as each test documents its setup and steps taken that led to the issue. 
  3. Identifying gaps in coverage and areas where additional tests or assertions are needed

Rapid Feedback Loop

The AAA framework inherently promotes test isolation by dividing each test into setup, action, and assertion stages. This structure inherently supports parallel testing, as the tests are designed to run independently of one another. As a result, the test suite runs quickly, and any issues are reported promptly to the development team, ensuring that problems are detected and resolved more rapidly.

Potential Hurdles: Challenges of AAA in Complex UI Testing

However, implementing the AAA model in UI testing is not without its challenges. One of the primary reasons for this is that it may be difficult to strictly adhere to the AAA framework for high-level tests. Some factors that contribute to this are:

Multiple Actions & Assertions

In UI testing, the "Act" phase can be quite complex. There are numerous actions, each with possible pitfalls. Ideally, you would want to assert after specific key actions to ensure you are on the right track. For example, after clicking "Add to Cart," it might be prudent to verify that the item count in the cart has increased.

Dynamic Elements

Modern e-commerce websites feature dynamic content loading, animations, and interactivity. Sometimes, you may need to wait for elements to load or become clickable, or you might have to manage unexpected pop-ups before taking a specific "action". This requirement doesn't fit seamlessly into the AAA structure without incorporating intermediate assertions or checks.

Multiple Points of Failure

There are many points at which the test can fail, and isolating a failure to a specific cause becomes challenging in a strict AAA structure. For example, in our "Add to Cart" example, if the search results don't return the "Harry Potter and the Sorcerer's Stone" book at all, the test will fail in the arrange segment itself. You might want intermediate assertions to narrow down potential problems.

In our experience, choosing the appropriate testing framework is similar to most software decisions you would make, as it requires its own trade-off analysis. Often, it is prudent to select a framework that works well for 80% of your use cases and be flexible for the remaining 20%. At Shaztra, we use the AAA framework in its essence and adapt for specific scenarios as needed. In fact, we use an enhanced version of the AAA framework - AAAC. Stay tuned for more on this in our upcoming articles.

Conclusion

The AAA framework provides a clear, structured testing paradigm. It's a valuable asset for teams prioritizing consistency and clarity across testing levels. By selecting a framework that works well for the majority of your use cases and adapting when necessary, you can optimize your testing process and ensure the quality of your software.

Which testing framework do you prefer for UI testing? Have you used the AAA framework in your UI tests? Share your experiences here.