Why and what is Arrange Act Assert?

Many years ago when I first started writing unit tests, I always got to a point where my tests started to look messy.

When I say messy what I mean is it could take longer than it should to understand

  • what was actually going on in a test
  • what data was being used
  • what was being tested
  • what the assertions were

The problem is worse if you were looking at a test you didn’t write!

I wasn’t the only one with this problem. Many examples in books, blogs and sample code had the same issue.

In 2003 William C. Wake blogged about using Arrange Act Assert pattern in unit tests.

From that moment on it was obvious to me that this was the only way to create tests that were

  • easy to read
  • follow
  • understand
  • maintain

It doesn’t matter if you’re someone starting out or a seasoned pro, if you start your unit tests with these three comments, it will help you structure your tests in a way that will allow you to focus on writing better unit tests.

Here’s an example to show what I mean.

If we wanted to test two numbers can be added together, we could write something like this

[Test]
public void should_be_able_to_add_two_numbers_together()
{
    int firstNumber = 1;

    Calculator calculator = new Calculator();

    int secondNumber = 2;

    var result = calculator.Add(firstNumber, secondNumber);

    result.ShouldEqual(3);
}

or we could apply the Arrange Act Assert pattern and have a test that looks like this

[Test]       
public void should_be_able_to_add_two_numbers_together()
{
    // Arrange
    int firstNumber = 1;           
    int secondNumber = 2;
    Calculator calculator = new Calculator();

    // Act
    var result = calculator.Add(firstNumber, secondNumber);
    
    //Assert
    result.ShouldEqual(3);
}

It’s much easier to see:

  • What is being set up and initialized in the arrange section
  • What method is being executed in the act section
  • What determines the outcome of the test in the assert section

If you have too much code in any of these sections, you’re probably breaking good unit test best practices by:

  • having too much setup
  • testing too many concerns
  • having too many assertions

If you’re using any kind of mocking framework then it’s even more important you follow a pattern like this because setting up what should be returned from a stub should be done in the arrange section. See my post about ‘How to write better unit tests using RhinoMocks and stubs‘ to see tests written this way.

I’ve heard this pattern being called build, operate, check amongst other things but nothing sticks in your mind better than AAA. There no ambiguity into what the three words mean and it’s so easy to remember.

And it’s not only me that likes the pattern, I’ve been to presentation were developers like Scott Gu have used it so it must be good ;)

All I can say is try it, there no cost, it doesn’t depend or any particular language or test framework and at worst you only need delete three lines of comments.