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?
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:
I suggest
MyClassTests
.Classes should be noun phrases, so commonly used
MyClassTest
and less commonMyClassTests
orMyClassTestCase
orMyClassTestFixture
all work. Technically, an instance of a JUnit test class represents a test fixture, butTestFixture
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).i prefer the suffix: TestCase. this is consistant with: http://xunitpatterns.com/Testcase%20Class.html
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 :)
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.
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.