在The Art of Unit Testing With Examples in .NET的第二章“A first unit test”中,提出了一些unit test应该注意的初步细节,比如测试方法命名的基本规则:
待测试对象:Project , 例如:项目名为Logan
测试代码中的对应对象:创建项目名称为[ProjectUnderTest].Tests;上例对应项目名为:Logan.Tests
待测试对象:Class,例如:类名为LogAnalyzer
测试代码中的对应对象:对于每个class,至少创建一个对应class,命名为[ClassName]Tests;上例对应类名为:LogAnalyzerTests
待测试对象:Method,例如:期待名为IsValidLogFileName的方法,输入合法的文件名,希望返回true
测试代码中的对应对象:对于每个方法,创建至少一个测试方法,命名规则为[MethodName]_[StateUnderTest]_[ExpectedBehavior]. 上例对应方法名为:IsValidFileName_valideFile_ReturnsTrue()
此外,还提到了针对状态的间接测试——state-based testing,其定义如下:
State-based testing (also called state verification) determines whether the exercised method worked correctly by examining the state of the system under test and its collaborators (dependencies) after the method is exercised.