Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 您如何命名您的单元测试方法?_Unit Testing - Fatal编程技术网

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