Java 命名约定 JUnit 后缀或前缀 Test
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3146821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Naming convention JUnit suffix or prefix Test
提问by aron
Class under test MyClass.java JUnit test case name alternatives:
被测类 MyClass.java JUnit 测试用例名称替代:
TestMyClass.java
MyClassTest.java
http://moreunit.sourceforge.netseems to use "Test" as prefix default but I have seen both uses. Both seems to be recognized when running the entire project as unit test in eclipse as it is the annotation inside classes that are parsed for @Test. I guess maven does the same thing.
http://moreunit.sourceforge.net似乎使用“Test”作为前缀默认值,但我已经看到了这两种用法。在 Eclipse 中将整个项目作为单元测试运行时,两者似乎都被识别出来,因为它是为 @Test 解析的类内部的注释。我猜 maven 也做同样的事情。
Which is preferred?
哪个是首选?
采纳答案by Andreas Dolk
Another argument for suffix - at least in english language:
后缀的另一个论点 - 至少在英语中:
A class usually represents a noun, it is a model of a concept. An instance of one of your tests would be a 'MyClass test'. In contrast, a method would model some kind of action, like 'test [the] calculate [method]'.
一个类通常代表一个名词,它是一个概念的模型。您的测试之一的实例将是“MyClass 测试”。相比之下,方法会模拟某种动作,例如“测试 [该] 计算 [方法]”。
Because of this, I'd always use the 'suffix' for test classes and the prefix for test methods:
因此,我总是对测试类使用“后缀”,对测试方法使用前缀:
the MyClass test --> MyClassTest
test the calculate method --> testCalculate()
回答by Jon Skeet
I prefer to use the suffix - it means that looking down the list of files in a directory is simpler: you don't have to mentally ignore the first four letters to get to something meaningful. (I'm assuming you have the tests in a different directory to the production code already.)
我更喜欢使用后缀 - 这意味着查看目录中的文件列表更简单:您不必在精神上忽略前四个字母以获得有意义的内容。(我假设您已经将测试放在与生产代码不同的目录中。)
It also means that when you use Open Type (Ctrl-T) in Eclipse, you end up seeing both the production code and its test at the same time... which is also a reminder if you don'tsee a test class :)
这也意味着当你在 Eclipse 中使用 Open Type (Ctrl-T) 时,你最终会同时看到生产代码和它的测试......如果你没有看到测试类,这也是一个提醒: )
回答by krock
Prior to JUnit 4 it was common to name your test classes SomethingTest and then run JUnit across all classes matching *Test.java
. These days annotation driven JUnit 4, you just need to annotate your test methods with @Test
and be done with it. Your test classes are probably going to be under a different directory structure than your actual source (source in src/
test classes in test/
) so these days prefixes/suffixes are largely irrelevant.
在 JUnit 4 之前,通常将测试类命名为 SomeTest,然后在所有匹配的类中运行 JUnit *Test.java
。现在注释驱动的 JUnit 4,你只需要用它来注释你的测试方法@Test
并完成它。您的测试类可能位于与您的实际源(在 中的src/
测试类中的源test/
)不同的目录结构下,因此现在前缀/后缀在很大程度上无关紧要。
回答by Morten Lauritsen Khodabocus
Not to offend anybody, but I think it is fair to say that "moreunit" is much less known than JUnit, which is pretty much ubiquitous, and established the convention of suffixing test classes "Test".
无意冒犯任何人,但我认为可以公平地说“moreunit”比 JUnit 少得多,后者几乎无处不在,并建立了后缀测试类“Test”的约定。
Although JUnit4 did away with the necessity of following both class and method naming conventions (resp. "postfix Test" and "prefix test"), I think both are still useful for clarity.
尽管 JUnit4 取消了遵循类和方法命名约定(分别是“后缀测试”和“前缀测试”)的必要性,但我认为两者对于清晰起见仍然有用。
Imagine the horror of having src/test/java/.../MyClass.myMethod() tested by src/main/java/.../MyClass.myMethod()...
想象一下 src/test/java/.../MyClass.myMethod() 被 src/main/java/.../MyClass.myMethod() 测试的恐怖...
Sometimes, it is useful to diverge from the JUnit3 conventions - I find that naming setup methods after what they do ("createTestFactory()") and annotating them "@Before" is much clearer than the generic "setUp()".
有时,偏离 JUnit3 约定很有用 - 我发现在它们所做的之后命名设置方法(“createTestFactory()”)并注释它们“@Before”比通用的“setUp()”更清晰。
This is particularly useful when several unrelated setup actions need to be performed - they can be in separate methods, each tagged @Before. This communicates the independence of the actions very nicely.
当需要执行几个不相关的设置操作时,这特别有用 - 它们可以在单独的方法中,每个方法都标记为 @Before。这很好地传达了动作的独立性。
回答by Jeanne Boyarsky
I also use MyClassTest_XXX when I want to split my test into multiple classes. This is useful when testing a big class and I want the tests logically grouped. (Can't control legacy code so this scenario does come up.) Then I have something like KitchenSinkTest_ForArray, KitchSinkTest_ForCollection, etc.
当我想将我的测试分成多个类时,我也会使用 MyClassTest_XXX。这在测试大类时很有用,我希望测试按逻辑分组。(无法控制遗留代码,所以会出现这种情况。)然后我有类似 KitchenSinkTest_ForArray、KitchSinkTest_ForCollection 等的东西。
回答by Ray Tayek
i prefer the suffix: TestCase. this is consistant with: http://xunitpatterns.com/Testcase%20Class.html
我更喜欢后缀:TestCase。这与:http: //xunitpatterns.com/Testcase%20Class.html 一致
回答by Pablo Jomer
I prefer using the TestClassName syntax. When using the other syntax I have trouble identifying which is the test and which is the actual class in editors when I have both open. Having to look for the Last four letters in the name is tiresome and also these letters are not always displayed.
我更喜欢使用 TestClassName 语法。使用其他语法时,我无法确定哪个是测试,哪个是编辑器中的实际类。必须查找名称中的最后四个字母很烦人,而且这些字母并不总是显示出来。
For me the other syntax leads to several wrong swapping′s between files every day and that is time consuming.
对我来说,其他语法导致每天在文件之间进行几次错误的交换,这很耗时。
回答by eytanfb
I think it is important you feel comfortable with your tests if you are working alone. But if you're in a group, you better sit down and get something fixed. I personally tend to use suffix for classes and prefix for methods and try to have my groups adapt to this convention.
我认为如果您独自工作,您对测试感到满意很重要。但是如果你在一个小组中,你最好坐下来解决一些问题。我个人倾向于使用类的后缀和方法的前缀,并尝试让我的组适应这个约定。
回答by DKroot
I suggest MyClassTests
.
我建议MyClassTests
。
Classes should be noun phrases, so commonly used MyClassTest
and less common MyClassTests
or MyClassTestCase
or MyClassTestFixture
all work. Technically, an instance of a JUnit test class represents a test fixture, but TestFixture
is a bit too verbose for me.
类应该是名词短语,所以常用MyClassTest
而不太常用MyClassTests
或MyClassTestCase
或MyClassTestFixture
全部工作。从技术上讲,一个 JUnit 测试类的实例代表一个测试装置,但TestFixture
对我来说有点过于冗长。
I think that MyClassTests
conveys intent in the best way because there are typically multiple test methods in a class each representing a single test (test case).
我认为这MyClassTests
以最好的方式传达了意图,因为一个类中通常有多个测试方法,每个方法代表一个测试(测试用例)。