Chicken or Egg? (Unit Testing)

by bluemedora_editor on June 23, 2011

While doing test driven development this is an extremely interesting question, well at least for some programming languages.

What I am referring to here is the idea of a failing tested with no source written, followed by a bit of source to make the test pass. When I say no source, what I mean is no source, although we all know that this isn’t possible in a programming language such as Java, C#, etc. In these languages we all know that it is impossible to reference something that does not exist and that is why I am talking about this. I will provide examples in java just for convenience sake, (Since I have my IDE up and going as we speak.) with JUnit as my testing framework. There are several rules that I tend to follow while writing tests.  Hopefully they will help.

  • Write the test 

 

@Test void realRealWinner( ) throws Throwable {                                     Winner target = new Winner( ) ;                                                    AssertTrue(“Winner did not win”, target.isWinner( ) ) ;

 }


UT-OH! This does not compile, because java requires that thereference to the class you are testing exists:( Let’s Fix it!

  • Fix the broken compilation by creating the class and the current method we are testing.
Public Class Winner {                                                               public Boolean isWinner() {                                                        //  The  body of the function is step 3                                                  }                                                                                              }

 

  • Hold on there… You might be thinking to yourself “why is this it’s own step, it’s just a method body right?” Well, you are exactly correct, but the thing I want to portray here is that if you were to put any type of return in the function to make the test pass (initially) could potentially poison expected results. For instance if you were to return false, and then were to step away for a moment, or better yet hand the keyboard off to your pair, and context is lost, there could be a resulting failure point in your implementation. I strongly urge that you do the following:

Public Class Winner {                                                               public Boolean isWinner( ) {                                                          throw new NotImplementedException(); // golden nugget                                                         }                                                                                                                       } 
The reason I do this is so that if this were to slip into production, or if I were to walk away, there is a clear statement that this method/section of code is indeed “NOT IMPLEMENTED YET”
  • Make the test pass.
  • Repeat steps one through four until you are finished with the current class / unit.

Hope this helped,  Jeremy Rowe

 

Get started

Try BindPlane for free. No credit card required.

Sign up
True Visibility
BindPlane for VMware vRealize Operations

True Visibility allows cloud management teams to use VMware vRealize’s powerful machine learning and capacity planning engine across their entire hybrid cloud environment.

Azure Monitor...everything
BindPlane for Microsoft Azure Monitor

Make Azure Monitor your first-pane-of-glass across your entire multi-cloud, multi-database or hybrid platform environment.

Thank you for contacting us. Your information was received. We'll be in touch shortly.