Premature Passes: Why You Might Be Getting Green on Red



Red, green, refactor. The first step in the test-driven development (TDD) cycle is to ensure that your newly-written test fails before you try to write the code to make it pass. But why expend the effort and waste the time to run the tests? If you're following TDD, you write each new test for code that doesn't yet exist, and so it shouldn't pass.

But reality says it will happen--you will undoubtedly get a green bar when you expect a red bar from time to time. (We call this occurrence a premature pass.) Understanding one of the many reasons why you got a premature pass might help save you precious time.
  • Running the wrong tests. This smack-your-forehead event occurs when you think you were including your new test in the run, but were not, for one of myriad reasons. Maybe you forgot to compile it, link in the new test, ran the wrong suite, disabled the new test, filtered it out, or coded it improperly so that the tool didn't recognize it as a legitimate test. Suggestion: Always know your current test count, and ensure that your new test causes it to increment.
  • Testing the wrong code. You might have a premature pass for some of the same reasons as "running the wrong tests," such as failure to compile (in which case the "wrong code" that you're running is the last compiled version). Perhaps the build failed and you thought it passed, or your classpath is picking up a different version. More insidiously, if you're mucking with test doubles, your test might not be exercising the class implementation that you think it is (polymorphism can be a tricky beast). Suggestion: Throw an exception as the first line of code you think you're hitting, and re-run the tests.
  • Unfortunate test specification. Sometimes you mistakenly assert the wrong thing, and it happens to match what the system currently does. I recently coded an assertTrue where I meant assertFalse, and spent a few minutes scratching my head when the test passed. Suggestion: Re-read (or have someone else read) your test to ensure it specifies the proper behavior.
  • Invalid assumptions about the system. If you get a premature pass, you know your test is recognized and it's exercising the right code, and you've re-read the test... perhaps the behavior already exists in the system. Your test assumed that the behavior wasn't in the system, and following the process of TDD proved your assumption wrong. Suggestion: Stop and analyze your system, perhaps adding characterization tests, to fully understand how it behaves.
  • Suboptimal test order. As you are test-driving a solution, you're attempting to take the smallest possible incremental steps to grow behavior. Sometimes you'll choose a less-than-optimal sequence. You subsequently get a premature pass because the prior implementation unavoidably grew out a more robust solution than desired. Suggestions: Consider starting over and seeking a different sequence with smaller increments. Try to apply Uncle Bob's Transformation Priority Premise (TPP).
  • Linked production code. If you are attempting to devise an API to be consumed by multiple clients, you'll often introduce convenience methods such as isEmpty (which inquires about the size to determine its answer). These convenience methods necessarily duplicate code. If you try to assert against isEmpty every time you assert against size, you'll get premature passes. Suggestions: Create tests that document the link from the convenience method to the core functionality, demonstrating them. Or combine the related assertions into a single custom assertion (or helper method).
  • Overcoding. A different form of "invalid assumptions about the system," you overcode when you supply more of an implementation than necessary while test-driving. This is a hard lesson of TDD--to supply no more code or data structure than necessary when getting a test to pass. Suggestion: Hard lessons are best learned with dramatic solutions. Discard your bloated solution and try again. It'll be better, we promise.
  • Testing for confidence. On occasion, you'll know when you think a test will generate a premature pass. There's nothing wrong with writing a couple additional tests: "I wonder if it works for this edge case," particularly if those tests give you confidence, but technically you have stepped outside the realm of TDD and moved into the realm of TAD (test-after development). Suggestions: Don't hesitate to write more tests to give you confidence, but you should generally have a good idea of whether they will pass or fail before you run them.
Two key things to remember:
  • Never skip running the tests to ensure you get a red bar.
  • Pause and think any time you get a premature pass.

86 comments:

  1. I would never want to miss out any opportunity to read out your contents.
    morgand24

    ReplyDelete
  2. If you are being attentive to learn several strategies then you ought to browse this article, I am certain you'll get much additional from this article. payday loans online

    ReplyDelete
  3. I have spent a lot of the time in different blogs but this is really a unique blog for me.Healthy Relationship with Bare Factory

    ReplyDelete
  4. Here at this site really the fastidious material collection so that everybody can enjoy a lot. PPIClaimz.org

    ReplyDelete
  5. Great webpage buddy, I am going to notify this to all my friends and contacts as well. insurance rates for cars

    ReplyDelete
  6. I was looking for the blogs and sincerely I have got the good work here. alpha technologies

    ReplyDelete
  7. Awesome blog! Now In anticipation of a follow-up …. android phone

    ReplyDelete
  8. I’m inspired with the surpassing and preachy listing that you furnish in such little timing.
    Get more info about Experience Spaces

    ReplyDelete
  9. Nicest information!!! I'll be enchanted to greatly help due to what I've learnt from here. cheap car insurance

    ReplyDelete
  10. I’m glad to find so many useful and informative data on your website.
    Go to 01 Mei

    ReplyDelete
  11. It feels awesome to read such informative and unique articles on your websites.
    ppi reclaim

    ReplyDelete
  12. I don't cerebrate some of websites offer this typewrite of assemblage. Auot insurance quotes

    ReplyDelete
  13. I actually found this blog and that is amazing thing I enjoy reading this easy to understand stuff. Keep it up.
    visitor counter

    ReplyDelete
  14. Fascinating information I haven’t been experienced such information in quite a long time.good infographics

    ReplyDelete
  15. An unbelievable blog. This blog will indisputably be definitely recommended to my friends as well.
    infographics design

    ReplyDelete
  16. The Info in the blog is out of this world, I so want to read more. infographic

    ReplyDelete
  17. I definitely respect and am grateful for your point on every single object.social media infographics

    ReplyDelete
  18. This is really an excellent blog as well as its content.
    personal payday loans

    ReplyDelete
  19. I truly get pleasure from while I read your blogs and its content. infographics design

    ReplyDelete
  20. I was searching the blogs for a long time and that is good to know that I have found such blogs here. Great working! Keep it up!
    whole life insurance quotes

    ReplyDelete
  21. I have been dotty by reading your blog because it has a unique data. infographic

    ReplyDelete
  22. Finally I’ve found your blog, awesome way of explaining each and everything really.
    whole life insurance

    ReplyDelete
  23. Your writers have capability to make understand the users, great stuff you have provided to us.auto insurance

    ReplyDelete
  24. Thanks for this post; it’s really amazing to read.cheap auto insurance

    ReplyDelete
  25. I really enjoy reading and also appreciate your work. car insurance

    ReplyDelete
  26. I really enjoy reading and also appreciate your work. car insurance

    ReplyDelete
  27. Thanks dear for the articles…. Really helpful for me and it’s according to the purpose.
    Ivy League tutoring

    ReplyDelete
  28. You hit realize out of the most other topics.
    german tutoring

    ReplyDelete
  29. It's not my very first time to visit this blog; I’m visiting this daily and acquire superb info from here day by day.compare auto insurance rates

    ReplyDelete
  30. The complete blogs are really inconceivable and definitely everyone will share this information.
    How to clean solar panels

    ReplyDelete
  31. Fastidious blog you’ve got here. I’ve ever been seeing you just about many blogs recently.
    online cash loan

    ReplyDelete
  32. I suppose I've selected an unbelievable and interesting blog.ee customer service

    ReplyDelete
  33. I want to say thank to you people for this great and helpful info. Thanks!!!
    vodafone customer services number

    ReplyDelete
  34. Hi, just desired to let you know, I enjoyed this blog post. It had been funny. Carry on posting!
    online personal loans

    ReplyDelete
  35. Your blogs are easily accessible and quite enlightening so keep doing the amazing work guys.compare automobile insurance quotes

    ReplyDelete
  36. The stuff written in the blogs have allured me!!! visit us

    ReplyDelete
  37. congratulations guys, quality information you have given!!! single premium life insurance

    ReplyDelete
  38. I think I have never seen such blogs ever before that has complete things with all details which I want. So kindly update this ever for us.
    new homes

    ReplyDelete
  39. Your articles helped me more in all kind of topics.best weight loss foods

    ReplyDelete
  40. Your blogs and its stuff magnetize me to return again n again. North Carolina cash loans

    ReplyDelete
  41. The information in this blog is extremely useful for the people.
    pirater un compte facebook

    ReplyDelete
  42. Hi, just desired to let you know, I enjoyed this blog post. It had been funny. Carry on posting!
    Detox Drinks

    ReplyDelete
  43. The information in this blog is extremely useful for the people.
    comment pirater un compte facebook

    ReplyDelete
  44. Fascinating information I haven’t been experienced such information in quite a long time. ACLS Renewal

    ReplyDelete
  45. http://www.gardenbloggers.com/2012/02/garden-bloggers-on-cutting-edge-example.html?showComment=1391074563051#c6189155632113524159

    ReplyDelete
  46. You people have actually provided the best blogs that are easy to understand for the folks. pirater un compte facebook

    ReplyDelete
  47. You people have actually provided the best blogs that are easy to understand for the folks. pirater un compte facebook

    ReplyDelete
  48. Hey to everyone, it’s my first visit of the blog site; this blog includes awesome and actually best info for the visitors. lexingtonlaw

    ReplyDelete
  49. Hey buddies, such a marvelous blog you have made I’m surprised to read such informative stuff.chestnut brown hair color

    ReplyDelete
  50. Wonderful, just what a blog it is! This blog has provided the helpful data to us continue the good work.
    vietnam visa

    ReplyDelete
  51. I have seen so many sites but never saw this type of work that has won my heart!!
    mango exporter pakistan

    ReplyDelete
  52. I wanna thanks to a great extent for providing such informative and qualitative material therefore often.
    the best universal life policies

    ReplyDelete
  53. You people have actually provided the best blogs that are easy to understand for the folks. ansonia bridal

    ReplyDelete
  54. It’s my first time to visit this site & I’m really surprised to see such impressive stuff out there.
    shipping companies UK

    ReplyDelete
  55. I'm in no doubt coming back again to read these articles and blogs.bubblegum casting

    ReplyDelete
  56. This is an informative blog by which I have got that info which I really wanted to get.
    http://www.epicfollowers.com

    ReplyDelete
  57. The information you have given in the blog really marvelous and more interesting.
    buy twitter followers UK

    ReplyDelete
  58. Cool blog site friend I'm about to suggest this to all my listing contacts.
    Winzip

    ReplyDelete
  59. Really amazing blog, I’d love to discover some extra information. http://www.epicfollowers.com

    ReplyDelete
  60. Really amazing blog, I’d love to discover some extra information.buy twitter followers UK

    ReplyDelete
  61. An enormous round of applause, continue the great work.

    ny central booking

    ReplyDelete
  62. Pretty remarkable post. I simply came across your blog and desired to say that I have really enjoyed searching your blog posts.
    vietnam visa

    ReplyDelete
  63. This info you provided in the blog that was really unique I love it!!!swiss replica watches

    ReplyDelete
  64. These blogs are quite incredible that have provided the best knowledge. ny criminal lawy

    ReplyDelete
  65. I further than doubtfulness revalue your articles and blogs.

    free media player download

    ReplyDelete
  66. The individuals not reading your blogs stuff are missing out much qualitative contents. bubblegum casting reviews

    ReplyDelete
  67. I'm delighted to mature so additional functional and informative collection on your web site.
    sewing kit

    ReplyDelete
  68. Your contents are wonderful and recommended. SEO Services

    ReplyDelete
  69. Enthralling stuff I haven't been finished particular data in a lasting time.
    best credit repair services

    ReplyDelete
  70. I really enjoy reading and also appreciate your work.

    adt security

    ReplyDelete