Code Aesthetics

Programming, agility, technical stuff ...

Acceptance testing with FitNesse and .NET

FitNesse is an open source software testing tool which can be used to do acceptance testing in a lot of different scenarios. It was originally developed to test java applications but can also be used with some modifications with the .NET framework. Since the documentation for using it in .NET is widely spread across the web I decided to write down some of the information that I have found useful in a few posts. Another useful resource of information is Gojko Adzic's book about Fitnesse. This post sums up briefly what you need to do in order to

  • ... install and run FitNesse
  • ... develop the integration layer
  • ... create a test page

Installing and running FitNesse

FitNesse is a Wiki which can be used to define and run tests which use data that is specified in a special table like syntax. To install and run FitNesse:

  1. Download fitnesse from fitnesse.org. You only need the file fitnesse.jar!
  2. Create a folder where the wiki should reside and copy the jar file to this folder. I use C:\Program Files\FitNesse for this example.
  3. If you do not have java, download and install java ( > jre6).
  4. Open a command window and run:
    cd C:\Program Files\FitNesse
    java -jar fitnesse.jar -p 8888
    This will unpack the fitnesse wiki and start running it on port 8888.
  5. To run .NET fixtures (tests) from FitNesse we need to install another test runnner. For this example FitSharp is used. It can be downloaded from here. Beware of the different versions for .NET 3.5 and 4.0.
  6. Unzip Fitsharp and copy it into your FitNesse folder.
  7. Make sure that the user which runs FitNesse has full access rights to the folder where it is located.

Developing the integration layer

Now it is time to develop the first FIT test.

  1. Create a new project with Visual Studio.
  2. Add the references to fit.dll and ftsharp.dll (if you followed the instructions they should reside in C:\Program Files\FitNesse)
  3. Create a fixture class (this example uses the ColumnFixture): [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace Samples.FitNesse { public class FizzBuzzFixture : fit.ColumnFixture { // object we want to test FizzBuzzer _fizzBuzzer; public FizzBuzzFixture() : base() { _fizzBuzzer = new FizzBuzzer(); } // inputs public int Number; // outputs public string Result() { return _fizzBuzzer.FizzBuzzConcat(Number); } } } [/csharp]
  4. ... and of course the class we want to test! [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Samples.FitNesse { public class FizzBuzzer { private int _n; private int _m; public FizzBuzzer() : this(3, 5) { } public FizzBuzzer(int n, int m) { _n = n; _m = m; } public string FizzBuzzConcat(int maxNumber) { StringBuilder resultBuilder = new StringBuilder(); FizzBuzz(maxNumber).All(s => resultBuilder.Append(s).Append(' ') != null); if (resultBuilder.Length > 0) { resultBuilder.Remove(resultBuilder.Length - 1, 1); } return resultBuilder.ToString(); } public IList<string> FizzBuzz(int maxNumber) { if (maxNumber <= 0) throw new ArgumentException("Only numbers bigger than zero are allowed"); List<string> words = new List<string>(); for (int currentNumber = 1; currentNumber <= maxNumber; currentNumber++) { if ((currentNumber % _n) == 0 && (currentNumber % _m) == 0) words.Add("FizzBuzz"); else if ((currentNumber % _m) == 0) words.Add("Buzz"); else if ((currentNumber % _n) == 0) words.Add("Fizz"); else words.Add(currentNumber.ToString()); } return words; } } } [/csharp]

Creating a simple test page

In order to create the first simple test page use your favourite browser and navigate to http://localhost:8888/SampleTest. This only works if fitnesse is started with the parameters specified in the first paragraph. If you can reach the page, edit the page and enter:

!define COMMAND_PATTERN {%m -r fitnesse.fitserver.FitServer,fit.dll %p}
!define TEST_RUNNER {Runner.exe }
!path ..\Samples.FitNesse\bin\Debug\Samples.FitNesse.dll

The path of course needs to be adjusted. Let it point to the project output path you have created for your tests. Now we can specify some test data.

!|Samples.FitNesse.FizzBuzzFixture|
|Number|Result?|
|1|1|
|2|1 2|
|3|1 2 Fizz|
|4|1 2 Fizz 4|
|5|1 2 Fizz 4 Buzz|
|16|1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16|

Save it and make sure that the page is flagged as a test in the page properties. Now you press the Test button and check out the test results.

FitNesse test result example

Summary

In this post I have described the steps to get FitNesse up and running with a small example (You can download the example here). In part 2 of this series I will describe how to setup FitNesse as a windows service and how to integrate fitness with CruiseControl.NET.

Johannes Täuber

I am a software architect and agility advocate. I am interested in all technologies out there and like to discuss options. My platform of choice is the .NET framework.