Naming convention JUnit suffix or prefix Test

JavaUnit TestingJunitNaming Conventions

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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

Solution 3 - Java

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.

Solution 4 - Java

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.

Solution 5 - Java

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.

Solution 6 - Java

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.

Solution 7 - Java

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.

Solution 8 - Java

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

Solution 9 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaronView Question on Stackoverflow
Solution 1 - JavaAndreas DolkView Answer on Stackoverflow
Solution 2 - JavaJon SkeetView Answer on Stackoverflow
Solution 3 - JavakrockView Answer on Stackoverflow
Solution 4 - JavaMorten Lauritsen KhodabocusView Answer on Stackoverflow
Solution 5 - JavaPablo JomerView Answer on Stackoverflow
Solution 6 - JavaeytanfbView Answer on Stackoverflow
Solution 7 - JavaJeanne BoyarskyView Answer on Stackoverflow
Solution 8 - JavaDmitriy KorobskiyView Answer on Stackoverflow
Solution 9 - JavaRay TayekView Answer on Stackoverflow