One of the most challenging aspects of software development is naming. In order to maximize the value of the tests we write, it is important to follow a naming convention that helps with quick understanding of test failures, grouping tests by functionality, and providing a simple yet meaningful approach to test naming.

The convention of MethodName_Given{Scenario}_Should{ExpectedResult} is preferred. This convention is easy to understand and allows for quick identification of the method being tested, the scenario being evaluated, and the expected outcome.

  • MethodName - The name of the method being tested on the object.
  • Given{Scenario} - A higher-level idea or context for the test. Avoid using specific technical terms if possible.
  • Should{ExpectedResult} - The expected outcome of the test. This helps the reader to understand the test without reading the method body.

C# Example

[TestCase(6)]
[TestCase(9)]
public class FizzBuzzer {    
    public string FizzBuzz(string input)
    {        
      //---------------Arrange-------------------
      var fizzBuzzer = CreateFizzBuzzer();
      //---------------Act----------------------
      var result = fizzBuzzer.GetResult(input);
      //---------------Assert-----------------------
      var expected = "Fizz";
      Assert.AreEqual(expected, result); 
    } 
  } 

For the following code snippet, a meaningful C# test name might be:

FizzBuzz_GivenMultipleOf3_ShouldReturnFizz

  • FizzBuzz - The name of the method being tested.
  • GivenMultipleOf3 - A higher-level idea or context for the test. This suggests that the method is being tested with a number that is a multiple of 3.
  • ShouldReturnFizz - The expected outcome of the test. This helps the reader to understand what the test is checking for.