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.

53 comments:

  1. I am happy to see that you have provided such an incredible and impressive blog for us. best home security companies

    ReplyDelete
  2. dentist reseda
    The information you have given in the blog really marvelous and more interesting.

    ReplyDelete
  3. phone lookup service
    The information you have given in the blog really marvelous and more interesting.

    ReplyDelete
  4. credit repair companies
    Each time I used to always check blog posts within the first hours in the break of day, because I like to get information increasingly more.

    ReplyDelete
  5. credit repair companies
    Each time I used to always check blog posts within the first hours in the break of day, because I like to get information increasingly more.

    ReplyDelete
  6. Criminal lawyer New York
    I have visited lots of the blogs but this blog is truthfully wonderful.

    ReplyDelete
  7. phone lookup service
    I would be supportive on all your articles and blogs as a result of they are simply up to the mark.

    ReplyDelete
  8. I am confident you've got a great enthusiast following there.
    top seo agency

    ReplyDelete
  9. No wonder why you receive countless of feedbacks.
    seo services

    ReplyDelete
  10. I’m flattened for your blogs writings and blogs as well. Medical Negligence

    ReplyDelete
  11. car insurance quote
    I'd like to take the power of thanking you for that specialized guidance I've constantly enjoyed viewing your blog.

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

    ReplyDelete
  13. write my essay for me
    Hi to everybody, here everyone is sharing such knowledge, so it’s fastidious to see this site, and I used to visit this blog daily.

    ReplyDelete
  14. Wonderful, just what a blog it is! This blog has provided the helpful data to us continue the good work. Flannel Duvet Cover King

    ReplyDelete
  15. Wonderful, just what a blog it is! This blog has provided the helpful data to us continue the good work. Flannel Duvet Cover King

    ReplyDelete
  16. Rolex Replica
    This blog post is really great; the standard stuff of the post is genuinely amazing.

    ReplyDelete
  17. teluate
    Hurrah, that’s what I was trying to get for, just what a stuff Presented at this blog!! Thanks admin of the site.

    ReplyDelete
  18. who's calling me
    Each time I used to always check blog posts within the first hours in the break of day, because I like to get information increasingly more.

    ReplyDelete
  19. Get More Info
    I was searching for many blogs site and now finally I have got this cool place with lots of information.

    ReplyDelete
  20. I absolutely respect and appreciate your point on each and every object. watch brands

    ReplyDelete
  21. write my essay
    I'm certainly very happy to read this blog site posts which carries plenty of helpful data, thanks for providing such information.

    ReplyDelete
  22. This is my very first time that I am visiting here and I’m truly pleasurable to see everything at one place. Loch Duart

    ReplyDelete
  23. This is my very first time that I am visiting here and I’m truly pleasurable to see everything at one place. Loch Duart

    ReplyDelete
  24. This is my very first time that I am visiting here and I’m truly pleasurable to see everything at one place. Loch Duart

    ReplyDelete
  25. watch
    Excellent Blog! I have been impressed by your thoughts and the way you

    ReplyDelete
  26. I believe this is actually the most useful blog I've been through this entire day. fashion

    ReplyDelete
  27. I believe this is actually the most useful blog I've been through this entire day. fashion

    ReplyDelete
  28. Hey buddies, such a marvelous blog you have made I’m surprised to read such informative stuff. Loch Duart Salmon

    ReplyDelete
  29. Hey buddies, such a marvelous blog you have made I’m surprised to read such informative stuff. Loch Duart Salmon

    ReplyDelete
  30. The caliber of information that you're offering is merely wonderful. best seo agency

    ReplyDelete
  31. The caliber of information that you're offering is merely wonderful. best seo agency

    ReplyDelete
  32. Thank you so much you have given the great blogs site by which we can get more advantage.
    life style

    ReplyDelete
  33. Your blogs and its stuff magnetize me to return again n again. life insurence

    ReplyDelete
  34. Excellent quality articles are here. This is good site with useful info. trial

    ReplyDelete
  35. Info is out of this world, I would love to read more. watch

    ReplyDelete
  36. Keep it up; keep posting more n more n more. european watch company

    ReplyDelete
  37. I absolutely respect and appreciate your point on each and every object. dating tips for women

    ReplyDelete
  38. I would be flattened if all websites gave articles like that. dating advice

    ReplyDelete
  39. Great job you people are doing with this website. tips for dating

    ReplyDelete
  40. These blogs are quite incredible that have provided the best knowledge.

    the lifestyle

    ReplyDelete
  41. Bundles of thanks for providing such kind of blogs with the informative and impressive articles. panties for sale

    ReplyDelete
  42. Bundles of thanks for providing such kind of blogs with the informative and impressive articles. panties for sale

    ReplyDelete
  43. These articles and blogs are truly enough for me for a day. Medical Negligence

    ReplyDelete
  44. congratulations guys, quality information you have given!!!
    healthy lifestyle

    ReplyDelete
  45. Loved your blog page!!!The stuff that you have remarked up here is superbly wonderful and I vigorously thank you for the same...
    Loch Duart Salmon

    ReplyDelete
  46. I have looked for so several posts about the blog lovers except this post is of course a nice one, keep on. http://panda-designs.com/rolex-replica/

    ReplyDelete
  47. I think each one is obtaining additional from this website, and your views are enjoyable in favor of new folks. Rolex Replica

    ReplyDelete
  48. I would never crave to lose out any chance to look throughout your contents. cosmetic dentist south bay

    ReplyDelete
  49. I would never crave to lose out any chance to look throughout your contents. cosmetic dentist south bay

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

    ReplyDelete
  51. Really amazing blog!!! I enjoyed the complete article? enormous written. dating advice

    ReplyDelete
  52. Really amazing blog!!! I enjoyed the complete article? enormous written. dating advice

    ReplyDelete