DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Leveraging Test Containers With Docker for Efficient Unit Testing
  • Mastering Test Code Quality Assurance
  • How To Make Legacy Code More Testable
  • Selecting the Right Automated Tests: A When and What Guide To Implementing Tests in Your Application

Trending

  • Contextual AI Integration for Agile Product Teams
  • Optimizing Integration Workflows With Spark Structured Streaming and Cloud Services
  • Power BI Embedded Analytics — Part 2: Power BI Embedded Overview
  • Mastering Advanced Aggregations in Spark SQL
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. 7 Popular Unit Test Naming Conventions and Best Practices

7 Popular Unit Test Naming Conventions and Best Practices

In this quick-reference tutorial, discover key unit test naming strategies that are used by the majority of developers.

By 
Ajitesh Kumar user avatar
Ajitesh Kumar
·
Updated Mar. 12, 25 · Tutorial
Likes (31)
Comment
Save
Tweet
Share
747.0K Views

Join the DZone community and get the full member experience.

Join For Free

This article presents a compiled list of naming strategies that one could follow for naming their unit tests. It is intended to be a quick reference for unit test naming conventions. 

That said, to see more details, please feel free access one of these pages listed below for deeper information:

  • What are some popular naming conventions for unit tests?
  • Unit Tests Naming Best Practices
  • Given-When-Then Technique
  • How to Unit Test Stream Pipelines and Lambdas
  • CI/CD Pipeline Testing

The following list is comprised of seven popular unit tests naming conventions that are used by majority of developers and compiled from above pages:

Unit Test Naming Convention 1: MethodName_StateUnderTest_ExpectedBehavior

MethodName_StateUnderTest_ExpectedBehavior: There are arguments against this strategy that if method names change as part of code refactoring, then the test name like this should also change or it becomes difficult to comprehend at a later stage. Following are some of the examples:

  • isAdult_AgeLessThan18_False
  • withdrawMoney_InvalidAccount_ExceptionThrown
  • admitStudent_MissingMandatoryFields_FailToAdmit


Learn Software Testing and Automation*

*Affiliate link. See Terms of Use.

Unit Test Naming Convention 2: MethodName_ExpectedBehavior_StateUnderTest

MethodName_ExpectedBehavior_StateUnderTest: Slightly tweaked from above, and a section of developers also recommend using this naming technique. This technique also has the disadvantage that if method names get changed, it becomes difficult to comprehend at a later stage. The following is comprised of how tests in first example would read if named using this technique:

  • isAdult_False_AgeLessThan18
  • withdrawMoney_ThrowsException_IfAccountIsInvalid
  • admitStudent_FailToAdmit_IfMandatoryFieldsAreMissing

Unit Test Naming Convention 3: test[Feature being tested]

test[Feature being tested]: This one makes it easy to read the test as the feature to be tested is written as part of test name. Although, there are arguments that the “test” prefix is redundant. However, some developers love to use this technique. The following list is how the above tests would read if named using this technique:

  • testIsNotAnAdultIfAgeLessThan18
  • testFailToWithdrawMoneyIfAccountIsInvalid
  • testStudentIsNotAdmittedIfMandatoryFieldsAreMissing

Unit Test Naming Convention 4: Feature to be tested

Feature to be tested: Many suggest that it is better to simply write the feature to be tested because one is using annotations to identify the method as test methods. It is also recommended for the reason that it makes unit tests as an alternate form of documentation and avoids code smells. The following list is how tests in first example would read if named using this technique:

  • IsNotAnAdultIfAgeLessThan18
  • FailToWithdrawMoneyIfAccountIsInvalid
  • StudentIsNotAdmittedIfMandatoryFieldsAreMissing

Unit Test Naming Convention 5: Should_ExpectedBehavior_When_StateUnderTest

Should_ExpectedBehavior_When_StateUnderTest: This technique is also used by many as it makes it easy to read the tests. The following list is how tests in first example would read if named using this technique:

  • Should_ThrowException_When_AgeLessThan18
  • Should_FailToWithdrawMoney_ForInvalidAccount
  • Should_FailToAdmit_IfMandatoryFieldsAreMissing

Unit Test Naming Convention 6: When_StateUnderTest_Expect_ExpectedBehavior

When_StateUnderTest_Expect_ExpectedBehavior: The following list is how tests in first example would read if named using this technique:

  • When_AgeLessThan18_Expect_isAdultAsFalse
  • When_InvalidAccount_Expect_WithdrawMoneyToFail
  • When_MandatoryFieldsAreMissing_Expect_StudentAdmissionToFail

Unit Test Naming Convention 7: Given_Preconditions_When_StateUnderTest_Then_ExpectedBehavior

Given_Preconditions_When_StateUnderTest_Then_ExpectedBehavior: This approach is based on a naming convention developed as part of Behavior-Driven Development (BDD). The idea is to break down the tests into three part such that one could come up with preconditions, state under test, and expected behavior to be written in the above format. The following list is how tests in first example would read if named using this technique:

  • Given_UserIsAuthenticated_When_InvalidAccountNumberIsUsedToWithdrawMoney_Then_TransactionsWillFail

My personal favorite is naming unit tests based on the writing features of the class under test. It helps me to make sure that a class follows single responsibility. It also aids a great deal in code refactoring.

unit test Testing

Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Leveraging Test Containers With Docker for Efficient Unit Testing
  • Mastering Test Code Quality Assurance
  • How To Make Legacy Code More Testable
  • Selecting the Right Automated Tests: A When and What Guide To Implementing Tests in Your Application

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!