patterncsharpModerate
Better test names
Viewed 0 times
testbetternames
Problem
I have some simple tests written, however, the names are not good...I think, any suggestions on better names? I'm not really sure how these tests should be named.
I'm looking for a format or pattern I can follow.
I'm looking for a format or pattern I can follow.
[TestClass]
public class DoorTests
{
[TestMethod]
public void DoorIsNotVirtualIfNameStartsWithLetterOtherThanV()
{
var door = new Door {Name = "R143"};
Assert.IsFalse(door.IsVirtual);
}
[TestMethod]
public void DoorIsVirtualIfNameStartsWithLetterV()
{
var door = new Door {Name = "V001"};
Assert.IsTrue(door.IsVirtual);
}
}Solution
I find that Roy Osherove's test naming convention is easy to apply and provides suggestive names for the test methods.
Roy proposes that the test method names should be composed of three parts:
Following this convention, the methods in the
Following a convention like this can provides consistency and makes it easy to use and maintain the test code.
Also, having the three elements delimited by underscores provides good readability: In case a test fails, with only a glance, you are easily able to grasp all relevant information about the failure: what component failed? what was the failure scenario? what is the expectancy that was not met?
Roy proposes that the test method names should be composed of three parts:
- MethodName (or PropertyName, as in our case)
- StateUnderTest (a brief description of the scenario being tested)
- ExpectedBehavior
Following this convention, the methods in the
DoorTest class could be named:- IsVirtual_NameStartsWithLetterOtherThanV_ReturnsFalse
- IsVirtual_NameStartsWithLetterV_ReturnsTrue
Following a convention like this can provides consistency and makes it easy to use and maintain the test code.
Also, having the three elements delimited by underscores provides good readability: In case a test fails, with only a glance, you are easily able to grasp all relevant information about the failure: what component failed? what was the failure scenario? what is the expectancy that was not met?
Context
StackExchange Code Review Q#11635, answer score: 15
Revisions (0)
No revisions yet.