Naming unit test methods of overloads in Java
What is the most accepted way to name a unit test method when the target has overloads. Consider these methods:
doSomething();
doSomething(String);
How would you name the corresponding test methods? Would this be the most a开发者_如何学Cccepted way?
testDoSomething();
testDoSomethingString();
Do whatever makes things more readable for you and your co-workers, if any. I think it depends on what your other tests for that class are, but based on those two methods, here is what I would do:
Test methods that test doSomething():
- doSomething_void_success (this would be some test that tests a successful path)
- doSomething_void_fail (this would be some test that tests an incorrect path)
- doSomething_void_someOtherTest
Test methods that test doSomething(String):
- doSomething_String_success
- doSomething_String_fail
- doSomething_String_someOtherTest
I don't use the test prefix anymore, as JUnit 4 doesn't require it. I just use the @Test annotation
There is no single "most accepted way" - pick what you(r team) feel is most readable and clean.
I personally don't use the test
prefix anymore, as it is not necessary since JUnit 4, and it degrades readability and searchability. I try to name my test methods after the scenarios they test. Which in your simplistic case could possibly be
doSomethingSuccessfully();
...
failsToDoSomethingWithAString();
...
doSomethingWithAStringAndFail();
I think it's the matter of your dev environment conventions.
I prefer to use underscores, as it allows cleaner representations of methods under test.
I also, still use test
prefix, though others pointed out that it's not required. I usually do this in order to separate actual tests from some helper methods that may be in the test class.
So, in your case, I would do
test_doSomething
test_doSomething_String
Use the desired behaviours to name your test methods, for example:
/**
*
* @return
* @should say hello, and nothing more that that
*/
String sayHello();
}
Would create a test method like this one:
@Test
public void sayHello_shouldSayHelloAndNothingMoreThatThat() throws Exception {
//TODO auto-generated
Assert.fail("Not yet implemented");
}
I have developed a plugin for IntelliJ IDEA (Eclipse version exists too) for creating these test methods for you, you can find it here:
http://plugins.intellij.net/plugin/?idea&id=5847
精彩评论