Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming convention JUnit suffix or prefix Test [closed]

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?

like image 465
aron Avatar asked Jun 30 '10 06:06

aron


People also ask

How do you name a JUnit test?

JUnit @DisplayName annotation is used to provide a custom name for the test class and test methods. We can use spaces, special characters, and even emojis in the display name.

Which is the proper naming convention of a test case?

Test class naming convention usually follows the naming convention of the class being tested, e.g., the class under test is “Order” the corresponding test class will be “OrderTests“. When in doubt, it is a good practice to model the test class name after the production class it is testing.

How should you name unit tests?

Naming your tests The name of your test should consist of three parts: The name of the method being tested. The scenario under which it's being tested. The expected behavior when the scenario is invoked.

How do you name a test script?

If we write tests for a single class, we should name our test methods by using this formula: [the name of the tested method]_[expected input / tested state]_[expected behavior].


2 Answers

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() 
like image 199
Andreas Dolk Avatar answered Sep 24 '22 21:09

Andreas Dolk


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

like image 34
Jon Skeet Avatar answered Sep 24 '22 21:09

Jon Skeet