Unit Test Naming Conventions

When I started writing unit tests I followed the standard of stating the behavior under test in the name of the test method. It was something like what Roy Osherove talks about in this 2005 post and has the following format:

[MethodName_StateUnderTest_ExpectedBehavior]

It worked reasonably well most of the cases, but sometimes, probably due to an effort in maintaining the name within a reasonable length the results were less than satisfying or I would spend too much time trying to get the name right.

Having decided to try a different approach I searched the web looking to find out how others were tackling this situation and what unit test naming guidelines they were using. In general it would all come to the same descriptive method name until I found a very interesting read in this post by Jay Fields. He compares test names to glorified comments and talks about anonymous tests.

Completely anonymous tests would be something of an extreme measure for me and the unit test framework being used doesn’t even support it (NUnit). However it kind of inspired me to try my own simplified approach which ended up like this:

[MethodName_TestID]

and where each test method has an associated description clearly stating the behavior under test. This way the method name remains concise and in the case of a test failing I rely on the test runner report (Gallio) to include the associated description thus allowing to quickly understand what went wrong.

Gallio Failed Test Report
Gallio Failed Test Report

Throw in a visual studio snippet to quickly define a new unit test with the custom format and we are ready to go.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s