Test Driven Development across Java, JavaScript, and Perl.

Every iteration my team explores ways of reducing time required for QA/Testing. We are a small team and do not have dedicated resources for Quality Assurance. That being said, what we can do is reduce the amount of defects found during that time. The best way to do that is to follow a Test Driven Development (TDD) methodology where you write your test from requirements before writing application code.

Now of course you cannot test everything but you can test close to one hundred percent of requirement and code coverage.  Now Test Driven Development usually is done with Unit Test but it can be done with Functional Verification Test (FVT) and Performance Verification Test (PVT) as well. However FVT and PVT require a level of requirement elaboration/documentation that is not always available.  That being said Automated Unit Test done right will reduce a large amount of possible defects.

How to perform Test Driven Development?

Now I alread described Test Driven Development (TDD) above but I thought I would go a bit deeper first before jumping into implementation across languages.  TDD is more than writing a test before code. Rather it is a repetitive iterative process of adding changes in bite sized pieces.

Test Driven Development Workflow

I recommend the following path:

  1. Start by writing a test for the smallest requirement, defect, or change request that has little to no dependencies. The smaller the change the less understanding you will need of the larger picture.
  2. Run the test you just wrote. If it succeeds then the existing code supports it and you should move on to the next smallest requirement.
  3. Write just enough code to make all of the test pass. If you have a dependency on another component that has yet to be defined or written then create an interface and mock it (I talk more about this later).
  4. Refactor your code to make it cleaner and smaller while rerunning all the test to ensure you don’t break any functionality. This also removes any dead code from retired functional requirements.

The basic test

Although different Unit Test frameworks can behave differently the “effective layout of a test case ensures all required actions are completed, improves the readability of the test case, and smooths the flow of execution. Consistent structure helps in building a self-documenting test case. A commonly applied structure for test cases has (1) setup, (2) execution, (3) validation, and (4) cleanup.

 Yes I basically stole from the Wikipedia article but I couldn’t write it any better.

Testing what doesn’t exist

I mentioned before that If you have a dependency on another component that has yet to be defined or written then you should create an interface and mock it. “Mock Objects are simulated objects that mimic the behavior of real objects in controlled ways.” It is important to note that these objects do not perform that functionality of what they are mocking. Rather they simply provide expected output for expected input.

Across three languages

In order to demonstrate how to write unit test and mock objects across three languages I decided to write the same component three times in each language.

Demonstration Projects of TDD in Java, Javascript, and Perl

The component is a simple application to read an email from a string and look up the title in an external directory (i.e. LDAP). Now the external directory doesn’t exist so I use a Mock object to test the components.

Now I am going to talk a bit about performing Unit Test, mocking, reporting coverage in Perl, JavaScript, and Java.

Unit Testing Perl

Unit testing in Perl has been around for a long time. However I often describe Perl code as being pre or post 2008. That is when Adam Kennedy gave a talk about managing a massive amount of perl  code and why testable code is important.

http://blip.tv/oslo-perl-mongers/software-testing-on-a-massive-scale-1897370

My favorite quote from the talk is “Our ability to create software is limited, primarily, by our ability to test software.” I also love the references to the city of London’s 19th century sewers. I recommend every Perl programmer watch this presentation.  You can look at any Perl code and tell if the developer used principals from this talk.

Typically I use the Test Anything Protocol for testing Perl. It has an extension for JUnit reports which allow it to fit nicely into a Rational Team Concert Development suite. I should also reference Test::Mock::Class which I use for mocking objects in Perl.

Now perl is a very dynamic language where you may only be able to identify dead code during runtime. I typically use Devel::Cover to identify dead code.

 Unit Testing in JavaScript

There are numerous testing frameworks in Javascript. Since I mostly develop using the Dojo Toolkit I use DOH.  As for Mock Objects in JavaScript there are several libraries out there. However since Javascript does not support Interfaces or Abstract Classes I have to cheat. I create stub class where each required function throws an error. For instance in one of the examples I create this interface.

/*******************************************************************************
 * Licensed Materials - Property of IBM 
 * 5748-XX8 
 * © Copyright IBM Corp.
 * 1992, 1993 All Rights Reserved US Government Users Restricted Rights - Use,
 * duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *******************************************************************************/
define("example/javascript/LookupInterface", [ "dojo" ], function(dojo) {
	return dojo.declare("example.javascript.LookupInterface", [], {

		/**
		 * Lookup the person's title based on their email.
		 * 
		 * @param email
		 * @return The person's title
		 * @throws AssertionError
		 *             Person could not be found.
		 */
		getTitle:function(email){
			throw new Error("Interface function getTitle(email) has not been implemented.");
		},

		// Constructor function. Called when instance of this class is created
		constructor : function() {}
	});
});

On one hand you get the benefit of an interface but the failure to implement a function is only known during runtime which is bad. As always I am open to suggestions and feedback.

Code coverage also gets a bit complicated with Javascript. I have tried extensions to Firebug and a few other tools. Unfortunately understanding Dojo separate from my code has always proved troublesome. Someone recommended JSCover but I haven’t had a chance to try it yet.

Unit Testing in Java

JUnit is possibly the most popular Unit Testing frameworks. It is also included within Eclipse by default and there are tools provided to make Test Driven Development a first class citizen. There are numerous tutorials available on the topic.

For mock objects in Java I recommend Mockito. If you are using Eclipse for Java development then I also suggest using the EclEmma. It has outstanding integration but it doesn’t report against exceptions very well.

Conclusion and follow-up topics

Test Driven Development is a very powerful tool. Looking at code writen using this method there is an obvious difference.  Code is less buggy and easier to manage. You have confidence your code is sound without having to implement the entire application. I also want to mention Jurgen Appelo excellent article on implementing a requirement across multiple iterations TDD style.

Now I purposely left some topics out for future blog post. For instance as I mentioned earlier it is possible to use Functional Verification Test (FVT), Business Verification Test (BVT). Performance Verification Test (PVT) as part of Test Driven Development. You can do more than simple Unit Test but there are more than a few gotchas.

 

Using Rational Team Concert feeds in the IBM Notes Client.

One of the great things about Rational Team Concert is it brings an awareness to a Development team. Now outside of the CLM Dashboards and the thick clients that awareness continues in the forms of email and news feeds. Now everyone knows email but when left unchecked your Inbox can become unmanageable.

My personal preference is to use the news feed capability of Rational Team Concert. Not only does it provide notifications out of my inbox but it allows me to have greater control over what I want to see. I can take any work item query and turn it into a feed.

That being said I have had trouble for some time trying to get the Feed Reader in the IBM Notes Client to read the feeds from Rational Team Concert.

Note_Feed_Authentication_Error

The problem is the default authentication mechanism in CLM is form authentication rather than basic authentication. The IBM Notes Client does not make this easy.  Here are the steps I took to enable form authentication of feeds in the IBM Notes Client.

Creating the feed in Rational Team Concert.

In Rational Team Concert you can create a feed of nearly anything. Unfortunately creating a feed from the web interface is a bit difficult but from the client is quite easy.

  1. Start by creating a Work Item query of the news you are interested.  I should mention that there are other events that can be subscribed to other than work items.
  2. Right click the query in the Team Artifacts view and select “Subscribe to Query Feed.”
    RTC_Feed_Subscribe
  3. Retrieve the URL for that feed subscription by right clicking the new feed and selecting “Copy Feed URL.”
    Retereive a URL of a query feed

Enabling Form Authentication in the IBM Notes Client

  1. First thing is to create a new account for Form Authentication In your Notes Client by selecting to
    1. File
    2. Preferences
    3. Accounts
    4. New Account
  2. Set the following information
    1. Basics
      1. Account Name: <anything>
      2. Type: HTTP/HTTPS
      3. Server: https://<Server Host Name>
    2. Log in information
      1. Select “Use direct log in or single sign on.”
      2. Enter your user name and password.
    3. Advanced Properties
      1. Authentication URL: https://<Server Host Name>/<Context Root>/service/com.ibm.team.repository.service.internal.webuiInitializer.IWebUIInitializerRestService/j_security_check
      2. Authentication Type: J2EE Form (advanced)

IBM Notes Form Authentication Settings Conclusion

Now the feed you copied from the client should work in the IBM Notes client feed reader.

Note_Feed_Authentication_Success

The beauty of this is you can now create notifications with a greater amount of flexibility. For instance you can use a single feed to notify an entire team when a new Critical defect is submitted.

Writing Unit Test for Legacy Code

Sandro Mancuso recently published a video on “Testing and Refactoring Legacy Code.” One of the key benefits for writing test for legacy code is it allows you to understand the existing code while reducing risk in changing it.

It is a great video but a bit long so here are some of his suggestions.

  • Do not change production code unless it is covered by a test.
  • Start testing from shortest to deepest branch. The Shortest branch will require less understanding of the code base.
  • Start refactoring from deepest to shortest branch. 
  • Do not execute another class from the unit test. The test should only care about the class it is testing. Otherwise you may be testing components (database, external systems, etc…) that you don’t want to test right now. Refactor the Singleton, Static call, or object to a protected method (Seam) and overwrite it.
  • Feature envy is when one class/method has functionality that probably belongs in another class.
  • Guard clause is a parameter check and should be moved earlier.
  • Bring declaration of variables and the code that uses the variable closer together.

 

 

Documenting the past

My team is currently in the process of documenting a project we inherited. The project was a skunkworks effort and documentation wasn’t as important as proving that it works. The developer who wrote it is no longer on the project and isn’t the most pleasant person to interact worth so we are on our own. The interesting thing about this exercise is we are realizing how much lack of documentation cost a project.

The number one reason for documentation is so another developer can step in and keep the project running if you are hit by a bus, falling piano, or UFO. The big motivator for us is code refactoring and removing dead code becomes very hard without proper documentation.

I was watching a presentation by Jack Diederich on code reduction and one of the reasons he often hears against removing dead code is “It might be used…somewhere.”

As he points out when using a dynamic language it can be very hard to identify if code is called or not. He recommends adding a comment to indicate where code is called from. Using code coverage tools does help identify dead code but you need to have a full understanding of the use-cases or else you may accidentally remove code that addresses an edge case.

Documenting legacy components can be seen as an expensive part of a project but will ultimately reduce technical debt and save time later on.

Finding my color

When I first started developing web sites choosing a color was easy. There were not that many to choose from. Of course HTML at that time supported 16 million colors but any competent web developer would limit it to web safe colors. I remember working for a small startup and had to tell the head of marketing that the logo and branding colors were not web safe. The Visibone color guide came in very handy back then.

Today picking a color scheme for a web site is not as easy because you have significantly more colors to choose from. Now I should mention that not all colors appear the same on each monitor and people see shades of color differently. Radiolab did a great podcast on how we see and experience Colors.

[iframe width=”474″ height=”54″ frameborder=”0″ src=”http://www.radiolab.org/widgets/ondemand_player/#file=%2Faudio%2Fxspf%2F211119%2F;containerClass=radiolab”%5D

When I am developing sites for work it is easy because their are color guidelines I need to be in sync with. So here is my advice for picking colors for a personal site.

Start with one color

Most WordPress themes ask you to choose a plethora of colors (background, foreground, text color, title color, link color, etc…). You want to start with one color and select other colors that complement. The next question comes down to how to pick one color. Now people spend a lot of time and money to come up with a single color. There is a whole psychology behind picking colors.

Possibly one of the simplest way to pick a color is to start with an image that represents you.  I am an avid Scuba diver so I started with an underwater photo I took of a shipwreck. Oh the Places I've Been image

Now I am not a fan of background images. Frankly they can be extremely hard to do right and can add a good amount of size to a page. So I take the image and use a service like http://www.pictaculous.com/ to find out what colors I should consider.

Palette

Of the three colors selected I chose #001C4A.

Try out different schemes

I found my notes on color from Art class in College. There are plenty of different methods on choosing complementary colors which do not always translate well for websites. Even the tried and true 30/30/60 can look odd. Frankly you end up back where we started with color doesn’t always look the same and the color of the content (pictures, embedded players, etc…) can make that color look different.

What I like to do is just try different palettes that contain my color from popular color palettes sites like Kular, ColourLovers, ColoRatate, or Color Explorer. It is very simillar to the process of picking up swatches at Home Depot and attaching them to the wall to see if they work.

Lessons learned from project hand offs

Over the years I have started many project and handed them off to others both successfully and unsuccessfully. Now as developers we develop and document our code/project as though we are going to get hit by a bus, meteor, or falling piano the next day. Then we are shocked when another person looks at our code/documentation and can’t successfully pick up where you left off.  This weekend I performed a retrospective on my projects over the years and made a list of the following best practices.

Don’t expect the person owning your code to be like you.

I once had a developer tell me that his documentation is only meant to be a memory reference for him. This is obviously a bad practice however in many respects we document less for things we think is obvious. For instance I may write java code that will be handed off to another Java developer. Now I am a Test Driven Developer and utilize the JUnit library to verify all of my code. Now just because the developer accepting my code knows Java does not mean that he/she knows JUnit. So I may have each test case very well documented but that doesn’t mean the new developer knows how to run them.

I recently watched an interview with Alton Brown about writing a recipe. He recommended against documenting everything. He tried to document how to make a soufflé for aliens and gave up after 20 pages.  There needs to be context or shared experience for this to be feasible. He takes an interesting approach by applying cartography techniques to recipe writing.

Give the person a top 10 list

Just because the person will receiving responsibility for your project doesn’t mean they can absorb 8 months of knowledge in a week. Provide them a Top 10 list to keep the lights running for the project. It is also important to footnote this list with what is not included and would require deep knowledge. I once had a developer maintain a project with only that list only to have the project explode 5 months down the line. There needs to be an understanding that the list will not solve everything.

Give the person a fool proof work environment

Everyone’s development environment is different and a huge amount of time can be wasted trying to get your project working in their environment.

Get more than simple sign off

Most project management documentation emphasize the importance of obtaining sign-off from the other person before the hand off is complete. In my experience developers sign off on accepting projects like people accept iTunes EULAs. There needs to be more involved in accepting a sign off.

One technique I have done over the past few years is to give the new developer homework. If the developer can make an enhancement or fix a core defect then they know the code well enough. Performing a code review of their change doesn’t hurt either.

Go on vacation

There should be a sense of urgency to encourage the developer to take ownership. They do not need to be that self reliant if you are always reachable. I like to set a deadline where I will not be available. Now I should mention that you shouldn’t hand off a project and then go immediately on vacation.

Conclusion

Handing off a project to another person will force you to write better code and documentation. The best advice I can give is to have a backup person and hand-off code often. Some teams have adopted a process of handing off code every day (http://www.technologyreview.com/news/423451/making-smart-handoffs/).