你如何命名你的单元测试方法? [英] How do you name your unit test methods?
问题描述
命名单元测试方法的一般规则是什么?我相信很多人也对这个问题感到疑惑.假设,一个被测方法是
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
?GetCustomerByIdShudReturnCustomerWhenItSucceeds
?GetCustomerByIdShouldReturnNullWhenNotFound
?Get_Customer_By_Id
?
..等等等等.
有什么想法吗?谢谢.
推荐答案
我建议使用以下格式.这有助于通过查看测试方法名称来了解您究竟在测试什么.
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
这是使用 Roy Oshrove 建议的命名标准:http://osherove.com/blog/2005/4/3/naming-standards-for-unit-tests.html
This is using Roy Oshrove's suggested naming standard: http://osherove.com/blog/2005/4/3/naming-standards-for-unit-tests.html
这篇关于你如何命名你的单元测试方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!