Quiz: What’s the Best Test Method Name?
Which of the following names of test methods do you think to be the best?
(Notice that we could leave out “payment_” from the last name if it is clear from the context, i.e. from the fixture [a fancy name for test class] name.)
According to the holy book of Clean Code, the code should make visible the intent as much as possible. According to the testing guru Kent B., a test should be telling a story
to its reader – a story about how the code should be used and function.
According to these two and my own experiences from reading a lot of
(test) core written by other people, the last one is absolutely the
best. However you have the right to disagree and discuss
PS: I firmly believe that calling a test method “test()” should be punishable.
Source: http://theholyjava.wordpress.com/2011/12/13/quiz-whats-the-best-test-method-name/
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





Comments
Christian Schli... replied on Wed, 2011/12/14 - 8:07am
Unfortunately, my favorite answer is not available. It would be:
testPaymentFailsForUnknownUserId()
As you can see, I prefer the test prefix in the name. It is part of the intention that should be expressed by it.
Michal Xorty replied on Wed, 2011/12/14 - 8:14am
/** * Payment should fail for unknown user id */ public void testPaymentUnknownUserId() ...On the other hand I agree that fourth option is also quite good and if I were in team with such conventions, I'd get used to them.
Also, when I am testing some public API with lot of possible behaviours, I use mixed names like:
Tomasz Nurkiewicz replied on Wed, 2011/12/14 - 8:43am
I think this was the biggest advantage when switching from JUnit to ScalaTest. I can finally write:
test("payment should fail for unknown userId") { //test code } test("payment should fail for unknown userId, even when payment was accepted") { //test code } test("payment should be accepted when valid useId supplied") { //test code }Erwin Mueller replied on Wed, 2011/12/14 - 11:07am
If you switch to groovy you can write (with the classic JUnit):
Fabrizio Giudici replied on Wed, 2011/12/14 - 1:12pm
Fabrizio Giudici replied on Wed, 2011/12/14 - 1:15pm
Jakub Holý replied on Wed, 2011/12/14 - 5:01pm
Thank you all for the comments!
There is of course no absolutely best naming convention and everyone may pick whatever s/he prefers but for me the winners are Scala & Groovy.
Suck NR replied on Tue, 2011/12/20 - 11:09am
in response to: fabriziogiudici
I think the only reason why we say "should XXX" rather than "must XXX" is that 'should' has a nicer/softer sound to it; although technically I believe in these situations 'should' is a modernisation of 'shall' which has a similar imperativity as 'must'. And perhaps comes as a hangover from when requirement docs were 100000 pages long and written in legalese.
Separate point: " if people started looking at how TestNG is more powerful and flexible than JUnit...", if people started thinking before they typed... ooo my tool is better than yours cause it does 1001 things not a 1000 ;) Seriously the difference between those two is smaller than the gap between pages in a book - lets not just start a X is better than Y thread when the actual post is about method names not test tools. You've still got to read the actual java code so it doesn't matter how friendly the output of the tool is.