Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you name your unit test methods?

Tags:

unit-testing

What is your general rule in naming your unit test methods? I am sure a lot of people out there also wonder about this question. Suppose, a method under test is

public Customer GetCustomerById(int id)

How would you name your unit test methods?

GetCustomerById?
GetCustomerByIdTest?
GetCustomerByIdShouldReturnCustomer?
GetCustomerByIdShoudReturnCustomerWhenItSucceeds?
GetCustomerByIdShouldReturnNullWhenNotFound?
Get_Customer_By_Id?

..and so on and so forth.

Any thoughts? Thanks.

like image 726
Stack0verflow Avatar asked Oct 16 '25 22:10

Stack0verflow


1 Answers

I would suggest to use following format. This helps to know what exactly you are testing by just looking at the test method name.

[UnitOfWork_StateUnderTest_ExpectedBehavior]

GetCustomerById_WithValidId_ReturnsCustomer
GetCustomerById_WithInvalidId_ReturnsNull

This is using Roy Oshrove's suggested naming standard: http://osherove.com/blog/2005/4/3/naming-standards-for-unit-tests.html

like image 75
Adarsh Shah Avatar answered Oct 18 '25 17:10

Adarsh Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!