Naming convention JUnit suffix or prefix Test [clo

2019-03-09 19:42发布

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?

9条回答
▲ chillily
2楼-- · 2019-03-09 20:06

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()
查看更多
地球回转人心会变
3楼-- · 2019-03-09 20:08

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).

查看更多
时光不老,我们不散
4楼-- · 2019-03-09 20:08

i prefer the suffix: TestCase. this is consistant with: http://xunitpatterns.com/Testcase%20Class.html

查看更多
何必那么认真
5楼-- · 2019-03-09 20:15

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 :)

查看更多
Animai°情兽
6楼-- · 2019-03-09 20:17

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.

查看更多
太酷不给撩
7楼-- · 2019-03-09 20:19

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.

查看更多
登录 后发表回答