Archive for the 'Code' Category

XP Programming: Part 2 Testing is Fundamental

Sunday, October 8th, 2006

One of the cornerstones of the type of Extreme Programming (is it more Agile Modeling?) that I am interested in pursuing is unit testing. You start with your requirements and build use cases from them. You take your use cases and build unit tests that test the functionality displayed in the use case. I want everything to be documented and the documentation to flow all the way back to the requirements document. This way, you can pursue changed requirements through the use cases and unit tests down to the objects that support them.

To get started with unit testing, grab NUnit from http://www.nunit.org/ and take it for a spin. I have used NUnit in a project before, and loved the structure that it provided. Test-driven development is when you begin your process by writing tests that cover your requirements and then write code until they all pass. Start with a simple sample testing suite:

C#:
  1. using System;
  2. using NUnit.Framework;
  3. namespace Reservation
  4. {
  5.     [TestFixture]
  6.     public class PolyscopiaUnitTests
  7.     {
  8.        
  9.         [SetUp]
  10.         public void Init()
  11.         {
  12.         }
  13.        
  14.         [TearDown]
  15.         public void Destroy()
  16.         {
  17.         }
  18.        
  19.         [Test]
  20.         public void MigrateObject()
  21.         {
  22.             Assert.Fail("This is a test failure");
  23.         }
  24.     }
  25. }

and add tests that cover your requirements. Writing good unit tests is not as easy as it sounds, it takes time and effort. While you are creating your tests, you are also defining the solution set that your program will generate. As you develop your code, your goal should be to reduce the number of tests that fail.

So how do you integrate this into your NAnt build file? This is something I have never done before, but it turns out that you would simply add a section to compile your unit test dll and then change the test block as follows:

XML:
  1. <target name="compileUnitTests">
  2.     <csc target="library" output="${basename}UnitTests.dll" debug="${debug}">
  3.       <sources>
  4.         <include name="${basename}UnitTests.cs"/>
  5.       </sources>
  6.       <references>
  7.         <include name="System.dll"/>
  8.         <include name="System.Data.dll"/>
  9.         <include name="c:\Program Files (x86)\NUnit-Net-2.0 2.2.8\bin\nunit.framework.dll"/>
  10.         <include name="c:\Program Files (x86)\NUnit-Net-2.0 2.2.8\bin\nunit.core.extensions.dll"/>
  11.         <include name="c:\Program Files (x86)\NUnit-Net-2.0 2.2.8\bin\nunit.util.dll"/>
  12.         <include name="c:\Program Files (x86)\NUnit-Net-2.0 2.2.8\bin\nunit.testutilities.dll"/>
  13.       </references>
  14.     </csc>
  15.   </target>
  16.  
  17.  
  18.   <target name="test" depends="compile,compileUnitTests">
  19.     <nunit2>
  20.       <formatter type="Plain" />
  21.       <test assemblyname="${basename}UnitTests.dll"/>
  22.     </nunit2>
  23.   </target>

Be sure and note the reference section and the nunit2 section.

Another topic that I would like to pursue is Spec#, an extension to the C# language to allow for provable code. See: http://research.microsoft.com/specsharp/ for more information on it. I will be trying to decide how to integrate it into my process.

It is also good to note and acknowledge that the mechanisms that work for a single individual may not scale to a group of 5 or, worse yet, a group of 500. I am going to attempt to keep to procedures that appear to be scalable enough to handle groups of up to 20 programmers. I will also try and identify those pieces that I feel will have trouble scaling beyond that.

XP Programming: Part 1 A New Beginning

Sunday, October 1st, 2006

I have been spoiled by Visual Studio for far too long.  It is time to come back into the real world and begin using build tools like a grown up.  To assist me in my transition from code monkey to XP Programmer extrordinare,  I have enlisted the assitance of Pro .NET 2.0 Extreme Programming by Greg Pearman and James Goodwill on APress.

Being like one of those precotious children who wants desert before they have eaten, I will dive directly to the tools section.  I should understand the tools before I worry about the structure, right?  Surely they put the tools way back in chapter 6 at the behest of an Editor that was out of touch with reality?

Anyway, enter nant (nant-0.85-rc4 to be more precise).  Installing is simply downloading it from http://nant.sourceforge.net/, unzipping it, and adding the path to the bin directory to your PATH environment variable.

To use it simply create an xml file named .build with all of the options you want and type nant from a command prompt.  It will automatically pick up the .build file and do the default action.

Let us look at it by watching a project grow from a small seed into a giant redwood.  Today I begin (well, not actualy begin, more begin to document for posterity) the first project I have documented from beginning to completion.

I won't mention the type of project it is, or tell you what it is going to do, I will just let it unfold.

I will re-start the project with a basic NAnt build file:

XML:
  1. <?xml version="1.0" ?>
  2. <project name="Polyscopia" default="test"></p>
  3.  
  4. <property name="basename" value="Polyscopia"/>
  5.  
  6. <property name="debug" value="true"/></p>
  7.   <target name="clean">
  8.     <delete>
  9.       <fileset>
  10.         <include name="${basename}.exe"/>
  11.         <include name="${basename}.pdb"/>
  12.       </fileset>
  13.     </delete>
  14.   </target>
  15.  
  16.   <target name="compile">
  17.     <csc target="exe" output="${basename}.exe" debug="${debug}">
  18.       <sources>
  19.         <include name="*.cs"/>
  20.       </sources>
  21.       <resources>
  22.         <include name="*.resx" />
  23.       </resources>
  24.     </csc>
  25.   </target>
  26.  
  27.   <target name="test" depends="compile">
  28.     <exec program="${basename}.exe" basedir="."/>
  29.   </target>
  30.  
  31. </project>

This will take the .cs files and the resource files and build them into an executable. 

Awesome isn't it? Well, that will be it for part 1.  I am now going to go back and build unit tests for my project using NUnit and I will document my trials in Part 2.