Are comments recommended for unit testing signed with Arrange-Act-Assert?

I find the concept of separating the instructions of my unit tests as suggested in the AAA template.

I usually add heading comments so the tests look like this:

// Arrange int a = 1; int b = 2; // Act int c = a + b; // Assert Assert.AreEqual(3, c); 

But I'm curious if it's okay to include these comments in the headline?

... or is this what I should avoid?

 int a = 1; int b = 2; int c = a + b; Assert.AreEqual(3, c); 
+11
c # unit-testing arrange-act-assert
source share
2 answers

This does not seem to add much value as soon as the basic premise is clear. Since you mention C #, I suggest taking a look at the Art of Unit Testing for examples. IMHO correctly call unit test is more important than posting / acting / approving comments inside it. As stated in the book, if the test fails, if it is named correctly, you can often determine the cause of the regression if you know what changes were made recently.

+6
source share

I have benefited a lot from this. This (for me) looks cleaner, it’s immediately clear which parts of the test are done, and this somewhat enhances the pattern. So no, I don’t think you need to avoid this.

If your tests get really complicated, this is a separate issue. Even a six-line test can benefit from these comments. If you do not have a confirmation section because you are checking that an exception is thrown, then obviously do not include the assert comment.

I am always grateful that they are in the code that I am checking. I feel it saves my time.

0
source share

All Articles