Better test names

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.

[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);
    }
}

Answer

I like the BDD style test structure.

GivenAnObject
WhenIPerformAnAction
ThenIGetTheseResults

In C# this translates nicely to a namespace structure:

namespace GivenAnObject
{
  [TestFixture]
  class WhenIPerformAnAction
  {
    private MyObject result;

    [SetUp]
    public void GivenAnObject()
    {
      // CreateMyObject();
      When();
    }

    public void When()
    {
      result = MyObject.ExecuteAction();
    }

    [Test]
    public void ThenIGetAResult()
    {
      Assert.AreEqual(ExpectedResult, result);
    }
  }
}

When run in nunit tests that follow this paradigm will display very nicely as a well grouped set of tests which are testing the behaviour of the various parts of your program.

In your case these tests would be:

GivenADoor
WhenTheDoorStartsWithV
ThenTheDoorIsVirtual

GivenADoor
WhenTheDoorDoesNotStartWithV
ThenTheDoorIsNotVirtual

Attribution
Source : Link , Question Author : CaffGeek , Answer Author : Stephen

Leave a Comment