views:

108

answers:

6

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?

+12  A: 

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

Jon Skeet
I agree, I want to see the Thing and it's test ThingTest together in lists of projects.
djna
I'm with Jon, suffix.
jeff porter
+1 for the Open Type point
Paul Whelan
Very informative from a practical point of view +1 for the convenience it brings.
aron
+7  A: 

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()
Andreas_D
How to do this from a design point of view was what I was looking for.
aron
+1  A: 

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.

krock
They are not. Assume we have a model for a connector and this class is named `Connector`, than the model for a test to test a connector instance would be a `ConnectorTest`. To me that's pretty obvious. You're right from a pure technical perspective but naming should be related to design.
Andreas_D
Enlightening to know history. +1
aron
@Andreas_D, that is true. Usually a unit test class will centre on testing one java class so the unit tests for `Connector` will be in `ConnectorTest`, so in hindsight of my answer, prefix/suffixes still have some value.
krock
+1  A: 

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.

+1 for inspirational.
aron
A great idea from BDD (http://blog.dannorth.net/introducing-bdd): rather than naming test methods "@Test testFailOnNull() {...}", use the verb "should": "@Test shouldFailOnNull() {...}". I find this conveys a lot of information concisely. It avoids repeating "test", and reads better than "@Test failOnNull() {...}".
A: 

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.

Jeanne Boyarsky
A: 

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

Ray Tayek