Search code examples
c#unit-testingarrange-act-assert

Are heading comments recommended when unit testing with Arrange-Act-Assert?


I find the concept of partitioning the statements of my unit tests as suggested in the AAA pattern useful.

I tend to add heading comments so that the tests look like this:

// Arrange
int a = 1;
int b = 2;

// Act
int c = a + b;

// Assert
Assert.AreEqual(3, c);

But I am curious, is it normal to always include these header comments?

...or is this something which I should avoid?

int a = 1;
int b = 2;

int c = a + b;

Assert.AreEqual(3, c);

Solution

  • That doesn't seem to add much value once the basic premise is understood. Since you mention C#, I suggest taking a look at The Art of Unit Testing for examples. Naming a unit test correctly is more important IMHO than arrange/act/assert comments within it. As the book points out, when a test fails, if it is named well you can often deduce the cause of a regression directly if you know what changes were made recently.