The Arrange, Act, Assert (AAA) pattern is used in tests to help organise and clarify test code. It can also help to spot potential problems in test code if these three phases don’t seem to exist.
The Arrange phase is where the thing we’re testing (the system under test) is put into a known beginning state.
The Act phase is where we perform some action on the thing being tested.
The Assert phase is where we check that the results of the Act phase are as expected.
When first learning to use the AAA pattern, it can be helpful to start with 3 comments:
public void ShouldAddNumbers()
{
// Arrange
// Act
// Assert
}
These comments can help to focus on making sure there are three distinct phases.
While these comments are useful when trying to learn (or teach) AAA, they should not be needed in the final version of the test code.
Test code should ideally be as good as production code. One of the things that qualifies code as “clean” is the absence of useless/pointless comments.
Test code should be easy to read, it should not need the AAA comments to be left in to be able to be understood.
If you are using the comment-first approach to help you get started and learn the AAA approach that’s all well and good. However, once the test is written, these comments should usually be removed before the code is committed to source control.
Once the AAA comments are removed, it should still be clear what the flow of the test is. If it is not then the test code may need some changes to improve the readability.
SHARE: