Naming Tests
Listen to this article
Your business analysts have no doubt spent days coming up with test cases for an interest calculation. Writing it all out in nice, simple, plain english so you can understand what you need to implement. They've probably produced half-a-dozen scenarios for you to test against. Don't go and spoil it all by naming your method testInterestCalculation. I want to see testInterestCalculationWithFixedRateFor24Months or something similar.
I know there will be those who say that it's redundant information to encode into the test name. I mean just look at the code right? Phooey! Ever written a method named remove that actually added something instead? If the answer is yes, go directly to jail. Do not pass go. Do not collection a hundred dollars! If, like most sane people, the answer is no, that's because we name methods to indicate intention. We then look at the code to see how it's been implemented.
Ever spent what seemed like hours trying to come up with a good name for your class? Why bother? Again, because it's so important to communicate not only to others but also to yourself the role of the class, it's purpose for being. Without an appropriate name, it's often hard to know what the classess responsibilities should be.
If I go onto a project and need to get an idea of what the system does, the first place I look is the test suite. It should tell me almost everything I need to know about the implementation.
So do me a favour and spend a bit of time naming your test cases something meaningful. Please! I beg you. It'll stop me wasting your time asking lots of stupid questions ;-)
Comments
Hey Simon,
I like the names of my tests to tell me what the CUT **Should** do. i.e, the test names should describe the behaviour of the CUT. So, i would rename your tests to testShouldCalculateInterestWithFixedRateFor24Months
I can then use th agiledox intellij plugin and see exactly what the CUT Should do.
Oh, BTW, TDD should be renamed to BDD. Behaviour Driven Development. If you think in terms of what the CUT Should do, you are doing BDD! Will also stop the confusion w.r.t Testing and TDD, or should I say BDD ;-)
Take it easy mate.
Damian
Posted by: Damian Guy | March 13, 2004 09:37 PM
Shouldn't that be "Do not collect $200"? Or is this an Australian Monolopy variant?
In any case, I hear this and have to keep reminding myself of this every time...
Posted by: Jason Yip | March 14, 2004 09:20 AM
I do like BDD though as it could have many variants. I've been on many a project that was quite obviously BS Drive Development ;-)
But seriously, actually I've been playing with many variants on TDD recently and I do like the should. I was trying to remember the name of the project that converts test names to doco. Thanks for the reminder.
Cheers,
Simon
Posted by: Simon Harris | March 14, 2004 09:33 AM
Probably should have been $200 except that the exchange rate means we probably only get about USD100 ;-)
But yes you're quite right Jason. My mistake. LOL
Posted by: Simon Harris | March 14, 2004 09:34 AM
/*****************
This is function that tests the interest calculation with a fixed rate of interest for 24 months.
I assume a few things: ...
This test was written because we anticipated this: ...
******************/
void testInterest(){...}
I'm a firm believer in well-documented code. I also am not much of a fan of TDD. Is it perhaps the lack of code documentation that is bugging you, rather than the method names? I think that attention to style and the think-before-you-code approach to coding is simply going to save you time in the long-run.
Posted by: Westquote | March 18, 2004 11:07 AM
Hey WestQuote, do you really think a four line comment to communicate intent is superior to the correct naming of a method (whilst communicating the same intent)? I don't like typing that much - I hope you do.
Posted by: Mike Melia | March 19, 2004 09:55 PM
I always thought it was "do not collect 200 pounds" and, let's face it, the monopoly game is based in london so it pretty much has to be this way.
Okay, I guess I should stay focussed - my business analysts do not really get to see my junit code so they have no idea what the tests are called. We have embraced TDD but haven't gone the whole XP hog and brought BAs into the development process - yet.
Posted by: Sean (London) | April 28, 2005 01:30 AM
Indeed it shoul dhave been 200 somethings. The point was really not that the business nor even the BAs would ever see your JUnit code but that the test name should be meaningful so that you as the developer can match the test up with the requirements more easily.
Posted by: Simon Harris | April 28, 2005 01:46 AM