单元测试命名最佳实践 [英] Unit test naming best practices
问题描述
命名单元测试类和测试方法的最佳做法是什么?
This was discussed on SO before, at What are some popular naming conventions for Unit Tests?
我不知道这是否是一个很好的方法,但是目前在我的测试项目中,我在每个生产类和一个测试类之间都有一对一的映射,例如Product
和ProductTest
.
I don't know if this is a very good approach, but currently in my testing projects, I have one-to-one mappings between each production class and a test class, e.g. Product
and ProductTest
.
然后在测试类中,我有一些方法,这些方法带有要测试的方法的名称,下划线,然后是情况和我希望发生的事情,例如Save_ShouldThrowExceptionWithNullName()
.
In my test classes I then have methods with the names of the methods I am testing, an underscore, and then the situation and what I expect to happen, e.g. Save_ShouldThrowExceptionWithNullName()
.
推荐答案
我喜欢 [UnitOfWork_StateUnderTest_ExpectedBehavior]
它以结构化的方式提供了有关方法名称的所有信息.
It has every information needed on the method name and in a structured manner.
工作单元可以小到单个方法,一个类,也可以大到多个类.它应该代表此测试用例中将要测试并受到控制的所有事物.
The unit of work can be as small as a single method, a class or as large as multiple classes. It should represent all the things that are to be tested in this test case and are under control.
对于程序集,我使用典型的.Tests
结尾,我认为这种结尾非常普遍,并且对于类(以Tests
结尾)也是如此:
For assemblies I use the typical .Tests
ending, which I think is quite widespread and the same for classes (ending with Tests
):
[NameOfTheClassUnderTestTests]
以前,我使用Fixture作为后缀而不是Tests,但我认为后者更为常见,然后我更改了命名策略.
Previously I used Fixture as suffix instead of Tests but I think the latter is more common, then I changed the naming strategy.
这篇关于单元测试命名最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!