Thursday, August 5, 2010

My MXUnit TDD Perspective in CFBuilder (or CFEclipse for that matter)

First things first, if you are not doing Test Driven Development yet you should at least give it a try. Sparing all the technical aspects of why its good, its just a fun way to code.

In order for you to understand why I have my perspective setup this way for TDD a quick explanation of what TDD is in order. The basic principal of  Test Driven Development is to write unit tests before you write your implementation code.  For instance, if I wanted to write a function in an Account object to test if a username existed I'd start by writing a test called something like "TestIsUsernameUnique" in a CFC Called TestAccount.cfc.  Once that test is written it is run and should fail since the IsUsernameUnique method has not been implemented yet. This is an important step in TDD, if that test passes there is certainly something wrong with the test itself.  Once you have seen the test fail the implementation code (the actual IsUsernameUnique function) is written and the test is run, and re-run until the test passes.  

One great way to learn TDD is to watch TDD Kata's.  These are screencasts of people doing live test driven development.  I've always learned best by example so watching TDD Kata's has been invaluable to my learning of TDD.

Now that I've gotten that out of the way lets talk about how I have my development environment setup for Test Driven Development.   When I'm doing TDD there are 4 things that are important to me: 1 - my UML diagram, 2 - the Test code, 3 - the implementation code, and 4 - my MXUnit view.   Additionally I like to have my outline and console views handy so I can get a quick glance of what functions are in in the code I've written.

The first step in creating my TDD perspective is to minimize the navigator view and get rid of every other view.  This might be a good place to point out that you can always reset your CFBuilder or CFEClipse perspective back to its default state by going to Window -> Reset Perspective. The following image shows what my default ColdFusion perspective looks like before I clean up the screen:


This is what my perspective looks like after I clean up my screen:


Now that I have a blank canvas lets get my workspace setup for Test Driven Development. Assuming that you have the MXUnit eclipse plugin installed, open it by either clicking on the MXUnit icon on the top toolbar, or by going to Window -> Show View -> Other -> MXUnit -> MXUnit.  By default this should open on the right side of the screen as shown:


This isn't the ideal location for TDD so I'll move it to the bottom of my screen by dragging the tab towards the bottom of the screen until the outline fills the width of the eclipse screen and dropping it there as shown here:


This is useful when using TDD for a few reasons.  First you get more real estate to read the "Tag Context" of the MXUnit view so you can see exactly why your tests are failing.  Second, as you'll soon see I need all the horizontal real estate as I can get for my code.  

Once I have my MXUnit view anchored on the bottom of my screen I'll also do the same process to add the Outline and Console views next to my MXUnit view on the bottom.  I add these tabs because I like to be able to see what functions are available in the file I am working on to make sure that the function names in my test code and in my implementation code are the same.  I also like to see what MXUnit is doing behind the scenes in the console.

Now for the important part, the code.   If you haven't already created your implementation and test cfc's, use the Navigator (which was minimized so you'll need to click on the Navigator icon on the left of the screen to see it) to create them.  Now you should now have two code editor tabs open in your editor, one for your implementation code and one for your test code.  The next step is to get both of these files to display right next to each other.  Personally I like my test code to be on the right and my implementation code to be on the left, but the order really doesn't matter.  To do this grab the tab for either your test code or your implementation code and drag it towards the left side of the screen until  the outline fills half of your editor area and drop it there. The final workspace should look something like this:


Now you can see your test code, test results, and implementation code all on the same screen and are ready to become a TDD ninja.

One last step is to save this perspective so it's readily available for your TDD development.  Go to Window -> Save Perspective As and enter a name for it.
Fork me on GitHub