可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Class under test MyClass.java
JUnit test case name alternatives:
TestMyClass.java
MyClassTest.java
http://moreunit.sourceforge.net seems 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.
Which is preferred?
回答1:
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]'.
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()
回答2:
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't see a test class :)
回答3:
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.
回答4:
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".
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.
Imagine the horror of having src/test/java/.../MyClass.myMethod() tested by 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()".
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.
回答5:
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.
For me the other syntax leads to several wrong swapping´s between files every day and that is time consuming.
回答6:
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.
回答7:
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.
回答8:
I suggest 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.
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).
回答9:
i prefer the suffix: TestCase. this is consistant with: http://xunitpatterns.com/Testcase%20Class.html