Introduction

Naming tests in a robust and informative way is an important aspect of writing tests. The importance of correct naming is arguably more important than the naming of the application code. This is because although application code naming is important, the expressiveness of application code is sometimes sacrificed for the sake of brevity.

In the case of test code, however, the brevity of names is sacrificed for expressiveness. This is because test code is seen as a form of documentation. If a software engineer is introduced to the code, their familiarization process is significantly enhanced through the reading of the various test cases. In turn, understanding test cases are enhanced through descriptive naming.

The importance of correct naming

Unit tests are seen as a form of documentation that enables software engineers to familiarize themselves with the code. Therefore, from a pure documentation point of view, precise naming is extremely important. However, from the point of view of running tests themselves, naming holds no value. NUnit does not implicitly rely on any naming conventions to run tests. Therefore, names could be arbitrarily changed, and the tests would still run.

Unit test areas requiring good naming

There are four areas of test code where good naming is critical.

  • The names of the test projects.
  • The names of the files that are included in the test projects.
  • The name of the test classes that contain the test cases.
  • The name of the test cases that contain one or more assertions.

Universal best practices of naming

There is a set of universally understood best practices with regard to naming individual test cases. These include the following:

  • Test names must describe some form of specification requirement.
  • Test names must allude to some form of input and associated output.
  • Test names should allude to some form of context so they may be read standalone.

Naming projects, files, and classes

The following is the simplest and most effective way to name test project, test files, and test classes:

  • Projects: Test projects should be named the same as the application project that they test with the word, Test appended. For example, if the application code project is called Project, the test project name should be called ProjectTest.
  • Classes: Test classes should be named the same as the class that they test with the word, Test, appended. For example, if the application code class is called BankAccount, the test class should be called BankAccountTest.
  • Files: Since each file contains one class, the name of the files should follow the names of their containing classes. For example, if a test class is called BankAccountTest, the file within which it is contained should also be called BankAccountTest.

This naming convention is not universally adopted by the industry. For example, many practices advocate appending .UnitTest in the project name as opposed to merely appending the word Test. This has the added benefit of distinguishing them from other types of tests such as integration tests.

Commonly accepted naming conventions

Significant naming convention variation occurs at the test method level. These conventions, as well as some examples, are included below:

Naming convention Examples
Feature_being_tested not_an_adult_if_age_is_under_18
MethodName_StateUnderTest_ExpectedBehavior isAdult_AgeLessThan18_False
MethodName_ExpectedBehavior_StateUnderTest isAdult_False_AgeLessThan18
test[Feature being tested] testIsNotAnAdultIfAgeLessThan18
Feature to be tested IsNotAnAdultIfAgeLessThan18
Should_ExpectedBehavior_When_StateUnderTest Should_ThrowException_When_AgeLessThan18
When_StateUnderTest_Expect_ExpectedBehavior When_AgeLessThan18_Expect_isAdultAsFalse
Given_Preconditions_When_StateUnderTest_Then_ExpectedBehavior Given_UserIsAuthenticated_When_InvalidAccountNumberIsUsedToWithdrawMoney_Then_TransactionsWillFail

Given the simplicity of the first method naming convention, it is adopted in the remainder of this course. The most important practice to adhere to after selecting a test naming convention is to keep it consistent across your project.

Quiz your knowledge on naming conventions

For the following test code, answer the question that follows. Suppose the application code is in a project called MeasurementApplication and the test code is in a project called TestMeasurementApplication.

Get hands-on with 1200+ tech skills courses.