PleaseDoNotNameYourTestCasesLikeThis

P

I’ve delivered this as a lightening talk a couple of times at companies I’ve consulted at, but thought I’d knock out a suitably short blog post.

A lot of test cases I come across are named like this

WhenSomethingHappensThenSomethingShouldOccur

While the writing of a test in the first place should be applauded, naming them like this is a tad irksome.

By their nature, test case names will generally be longer than the method names their covering.
This in itself makes it harder to read – like a sentence without any spaces.

The pattern I prefer to follow is the one prescribed by Roy Osherove which looks like

UnitOfWork_StateUnderTest_ExpectedBehavior

The underscore separation is very important.
It acts like the space in a sentence; it makes it far easier to read these intentionally longer method names.

Also, this is not (I’d hope) how we’d ever name a real life method.

Thirdly, it makes it easy to distinguish while navigating through code using Resharper. Other go-to-anything tools are available.


I won’t paraphrase the other benefits, which are in the above linked blog post, but I encourage you to check it out.

Oh, and for those that are interested, my 6-deck long slides are available here:
https://docs.google.com/presentation/d/1upUeEnDPCsNzJQ_rh-Rhp65aHkIRTqeAJEwxUm-lZrw