21

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);
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Lea Hayes
  • 62,536
  • 16
  • 62
  • 111
  • 1
    You should avoid. Those comments add no value whatsoever. – k.m Sep 06 '14 at 06:42
  • 2
    @jimmy_keen I have to disagree on that one. In this case it's really easy logics, but as stated by others here, when you get big projects, with alot of test, which many people have written.. And looong tests maybe, those three comments are gold – Stígandr Sep 06 '14 at 08:41
  • 2
    @Stian.: "looong tests" indicate design problems. When you need *arrange, act, assert* comments to make your test understandable you're basically saying *"I haven't had time to clean this test method up so I leave you those markers so hopefully you can figure out what I meant with this poorly written code"*. Do yourself and your team a favor and sort it out. Clean code doesn't need comments. – k.m Sep 06 '14 at 14:18
  • 9
    I use this heuristic: http://blog.ploeh.dk/2013/06/24/a-heuristic-for-formatting-code-according-to-the-aaa-pattern – Mark Seemann Aug 08 '15 at 19:03

2 Answers2

16

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.

bright
  • 4,700
  • 1
  • 34
  • 59
7

I've gotten a lot of value out of doing this. It (to me) looks cleaner, is immediately clear which parts of the test are doing what, and it somewhat enforces the pattern. So no, I don't think you need to avoid it.

If your tests are getting really complicated that's a separate issue. Even a six line test can benefit from those comments. If you have no assert section because you're checking that an exception is thrown, then obviously don't include the assert comment.

I'm always thankful to have those in code that I'm reviewing, particularly for integration tests. I feel it saves me time.

b15
  • 2,101
  • 3
  • 28
  • 46
  • 1
    Test case are (and should be) very formulaic, they all follow the same pattern. I don't need someone to explain the pattern in every test case. When I see a test which has several lines of _mockBla.Setup(....) followed by _sut.DoSomething(....) and finally several lines of Assert.Something(.....) I don't need comments to understand what's what. – Phil B Sep 14 '22 at 19:33