TDD – Unit Test Naming Conventions Best Practices

Unit Testing Naming Conventions and Standards

I’m going to talk about simple and easy-to-understand Unit Test Naming Conventions and Best Practices that can be followed while writing Unit Testing.

As we know Naming standards in any format for anything ( code, documents, unit tests, etc) are always helpful in the long run. The naming standard brings uniformity and avoids ambiguity.

Today in this article, we will cover below aspects,

Coming to Unit testing naming, however, we don’t really need to get crazy. However, by following them we get multiple benefits.

Following such naming doesn’t require any extra effort, as once put into practice, you enjoy all their benefits.

In our last article, we understood general best practices for writing the unit test cases.

However today in this article we will focus primarily on the naming conventions

Today we will see how to use the naming convention for,

  • Test Method
  • Test Class
  • Test Project

Developer’s Confusion on Naming ??

This is a small but very common problem among developers.

There is always confusion in the Developer community on how to name and structure Unit Test cases properly.

Most often developer ends up writing their test cases without any guidelines resulting in issues like tests are not readable, not properly structured, and difficult in troubleshooting, especially on the build server where thousands of test cases run daily.

Unit Testing Naming Standards

Naming Pattern for Unit Testing

As we discussed in our last article on Unit testing best practices, it’s important to understand that while writing actual unit test cases everything is drilled down to finally Classes and Functions.

These two entities are required to be unit tested in isolation for a given functionality implemented in methods.

Unit Testing Naming Conventions and Standards

Unit Test Project Naming Convention

The below examples are just guidelines to help you with the naming pattern for the Test project.

You are free to use your own customized naming pattern as required. Below are just few suggestions based on my experience. 😉

Pattern:

[App-Name].Unit.Tests

Example: BooksApi.Unit.Tests

Unit Testing Naming Conventions and Standards

Other conventions like BooksApi-Unit-Tests can also be used if found suitable.

Unit Test Files Naming Convention

Files can be named after the class names you are dealing with …

Pattern:

[ClassName]Tests

Example: BookServiceTests.cs

Unit test name example

Tips:

  • Every class with the public method (at least one method) should have a test file created. (This also means you should refactor your files as per classes)
  • Make sure to write unit test cases for all public APIs for each given class.
  • Each test file should be completely isolated from other files.

Unit Test Class Naming Convention

You need a test class to encapsulate all your test cases. This test class provides an entry point to test cases depending on the configuration.

This also helps to initialize the test data required for test cases.

Pattern:

[ClassName]Tests

Example: ‘EmployeesTests’ where Employee is a class name.

Tips:

  • Name your Test Class as per the business class name.
  • Each Test Class should be defined in a separate test file as discussed above.
  • Each Test Class should have a mechanism to initialize mock or test data
  • Each test class should be public.

Unit Test Method Naming Convention

Each Unit test method should be unit tested in isolation. This isolation can be simplified by a Class and all its methods.

Pattern :

[UnitOfWork]_[Method]_[Behaviour]

Example: BookService_GetBookAsync_InValidBookID_Failure

Unit Test Naming Conventions Best Practices

Unit Test Structure

One can follow the AAA or Given-When-Then style pattern while structuring your test cases.

Pattern:

Act – Arrange – Assert

The essential idea of the AAA pattern is to break down writing your test into three sections.

  • Arrange: This describes a preconditioned state of the test before you begin to test the behavior you’re specifying in this scenario. Helps you create required test preparations, test data, mocking, etc.
  • Act– This section tests the behavior that you’re specifying.
  • Assert– This section describes the changes you expect due to the defined inputs through arranging and specified behavior through Act.

Unit Test Naming Conventions Best Practices

Some of you might like one more pattern as below,

Pattern:

Given-When-Then 

Unit Test Naming Conventions Best Practices

The above pattern has the same meaning on the structure as defined above for the AAA pattern.

Given-When-Then is most popular in Behavior-Driven Development (BDD) and Functional Testing.

Do you find the above conventions helpful?

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!

Summary

In this post, we learned a few basic and easy-to-use naming conventions for Unit Testing. We looked at the different naming convention which is useful for Test Project, Test Files, Test Class, Test method and Test method structure, etc.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.