Unit testing 您如何命名您的单元测试方法?
命名单元测试方法的一般规则是什么?我相信很多人也对这个问题感到好奇。假设测试中的方法是Unit testing 您如何命名您的单元测试方法?,unit-testing,Unit Testing,命名单元测试方法的一般规则是什么?我相信很多人也对这个问题感到好奇。假设测试中的方法是 public Customer GetCustomerById(int id) 您如何命名您的单元测试方法 GetCustomerById? getCustomerBydTest? getCustomerBydShouldReturnCustomer? getCustomerBydShoudReturnCustomer何时成功? GetCustomerBydShouldReturnNullWhen notf
public Customer GetCustomerById(int id)
您如何命名您的单元测试方法
GetCustomerById
?getCustomerBydTest
?getCustomerBydShouldReturnCustomer
?getCustomerBydShoudReturnCustomer何时成功
?GetCustomerBydShouldReturnNullWhen notfound
?通过Id获取客户
…等等等等
有什么想法吗?谢谢。我建议使用以下格式。这有助于通过查看测试方法名称来了解您正在测试什么
[UnitOfWork_StateUnderTest_ExpectedBehavior]
GetCustomerById_WithValidId_ReturnsCustomer
GetCustomerById_WithInvalidId_ReturnsNull
这是使用Roy Oshrov建议的命名标准:
太好了。谢谢这意味着这个getCustomerByd方法需要更多的单元测试方法。例如,带有InvalidId的GetCustomerById_应返回null,或者可能带有NegativeId的GetCustomerById_应通过WinValidCustomerId异常返回。对吗?是的,没错。每种测试方法只能测试一件事。它有助于维护和可读性。罗伊的那篇文章很好。谢谢你的链接。如果你感兴趣的话,你也可以读他的书。好的。这取决于您在这里尝试测试的内容。如果您只是测试它是否调用repository方法,那么类似于:GetCustomers\u with validReplicatory\u调用getAllMethodinCustomerRepository