What Do You Need to Know About AAA in C# Unit Testing
What Do You Need to Know About AAA in C# Unit Testing

What Do You Need to Know About AAA in C# Unit Testing

Do you write automated tests for your software?

If so, you’re probably familiar with the arrange, act, and assert (AAA) pattern, which is a useful way to structure your unit tests. It helps you isolate the specific thing you want to verify while making sure all the other bits are as you expect, too.

The AAA pattern is used to create unit tests in C# that are easy to read and understand:

  • Arrange: Prepare the class you want to test.
  • Act: Perform the action you are testing.
  • Assert: Check the result of the action you are testing.

This post is a quick introduction to the AAA pattern in C# unit testing.

What is arrange, act, and assert (AAA) in unit testing?

Automated tests are a great way to ensure your production code is working as intended. Unit testing is a type of automated testing that checks small pieces of your testable code. Unit testing is a dry run where you can fix problems before they become a problem.

It’s important to know that AAA, or Arrange, Act, Assert, is a pattern that you can use to test your C# code.

The three components of AAA are:

  • Arrange: Prepare the input for the test.
  • Act: Execute the code to be tested.
  • Assert: Verify that the expected output matches the actual output.

Let’s see one example of how to structure the test method to follow this pattern. The following test covers the biggest math problem everyone wants to test in their blog posts; adding two numbers.

[Fact]
public void AddTwoNumbers_returns_sum_of_numbers()
{
    //Arrange
    var calculator = new Calculator();
    //Act
    var result = calculator.AddTwoNumbers(5, 6);
    //Assert
    Assert.Equal(11, result);
}
  1. In the Arrange part of the unit test, we construct the system under test, the class we want to rest. In this case, that’s the Calculator class.
  2. In the Act part of the test case, we call the method we want to check, AddTwoNumbers.
  3. In the Assert part of the test, we check that the test result is as expected. We use the Assert.Equal method to check that the result is equal to 11. That method is a part of a xUnit framework, which provides us with the methods to make writing unit tests easier and faster.

How large should each section be?

The size of each section, Arrange, Act and Assert, is something to be considered and planned for. You want to make sure there is enough information in every test case but not too much to overwhelm the reader. These things are important because it takes the effort away from the reader, and leaves them with a clear understanding of the code.

The good practice when it comes to writing unit tests is as follows:

  • The Arrange section should be large enough to create a class you want to test and set up the test data. Ideally, a couple of lines.
  • The Act section should be one line. Call the method or unit you want to test, and that’s it.
  • The Assert section should ideally contain only one assert method to verify that the Act section has performed as expected.

This is a short overview. I’ll dive a bit deeper into each phase in the following sections.

What are the benefits of AAA?

There are many benefits of AAA in unit testing, but the main one is that it saves time.

It saves time because it helps developers write a test without thinking about how to structure the unit test method. With AAA, developers just follow the instructions that are provided to them.

In addition, you can use AAA for a variety of types of testing, including integration testing, system testing, and acceptance testing.

What are the drawbacks of AAA?

The drawback of this pattern is that it can lock you in thinking that every automated test must look the same.

If you always follow the same pattern, it can be difficult to solve new kinds of problems. For example, if you find that the sections of your code repeat throughout your test class, you may want to use Extract Method refactoring to place Arrange and Act phases into a separate method. In that case, your test might call that method and only have the Assert phase.

And that’s ok.

Arrange, or Preparation

The Arrange section of the unit test is the setup.

It’s where you lay the groundwork for what you’re testing and set up all the variables you’ll need. You need to make sure that all necessary components are in place to run your tests. This is where you create the system under test, set up a mocked or stubbed version of the service you need to use, most likely using a mocking framework. Here you want to isolate your code from external dependencies.

While there are many opinions on the correct length of the arrange section of a unit test, it can typically fall somewhere in the range of 5 to 10 lines of code.

One reason for this is that if the arrange section is longer than that, it may indicate that your test is trying to test two separate things simultaneously, requiring you to split it into two separate tests. The other issue you might have is that your system under test is complicated to set up and use.

Act, or Execution

The Act part involves executing the code you want to test. You can usually do this with one line of code.

The difference between Arrange and Act is in length and purpose. Arrange usually has several lines, while Act should only have one line. The purpose of Arrange is to create objects. The purpose of the Act is to call methods on those objects.

Assert, or Verification

The Assert part of the test code is the section where you actually verify that your code behaves the way you intend it to. It usually includes one assertion for each of the features or scenarios covered in the Act section.

How many assertions should the assert section hold?

A typical unit test has only one assert. You want to check one scenario, one fact about your code per test.

Can a unit test have multiple asserts?

Yes, it can. It’s ok to use multiple assertions as long the asserts check the various properties of the same result.

xUnit AAA test snippet

To help you use this pattern, I have created a small code snippet you can import and use in Visual Studio.

You can see the gist here.

You can also download the snippet using this link.

Just place the snippet in the snippets folder:

%USERPROFILE%\Documents\Visual Studio 2019\Code Snippets\Visual C#\My Code Snippets

and restart the Visual Studio. You can type testaaa to generate the following code structure automatically:

AAA unit test snippet

Summary

In this post, you have learned about Arrange, Act, and Assert (also known as AAA) parts of the test. This is the most common pattern for writing tests since it helps you write your code in small and organized chunks.