Adding FitNesse to your .Net Solution

This entry is part 3 of 5 in the series FitNesse

Sample Code

In the last post I mentioned Jeff Kwak’s blog post on FitNesse, specifically his way of creating a FitNesse wiki within your .Net solution.

The rest of the posts in this series will use a particular arrangement of projects based heavily on Jeff’s approach.

I’m not going to rehash his post, however I did have a few problems getting things to work the way Jeff did, and I also wanted to do some things a little differently. In this post I’ll discuss where my approach differs from his.

I’m providing a very basic Solution with FitNesse included and we’ll walk through creating and passing a first Test. Subsequent posts in this series will build incrementally on this Solution.

A Sample Solution
Download the attached zip file and extract it somewhere on your dev box. Mine is extracted to C:\Projects\FitNesseTutorial.

Open the resulting Solution. It has two Projects, Calculator (our system under test) and Calculator.Specifications (our tests).

Let me apologise right now for going down the well-trodden path of simple calculator examples. My goal with these first few posts is to get you using FitNesse in the simplest possible way. There will be time later to consider more realistic examples of how FitNesse might be used in real projects.

I’m not going to try and find some grand integrated application that illustrates all of the features of FitNesse. I will use simple standalone examples as required to best illustrate what I’m talking about.



Before we start worrying about FitNesse, build the solution. It should build successfully without any changes.

Setting up External Tools
This next step is optional, but I think it’s worth doing. I’d like a way to start and stop the FitNesse server from within Visual Studio, and pop up a browser running the Wiki, also from within the IDE. What I’ve got is not as clean as I’d like, but it’ll do until I find a better way.

Starting FitNesse
The sample Solution includes a batch file that runs FitNesse. The first thing we’re going to do is create an External Tool in Visual Studio to run that batch file.

In the External Tools Dialog, Add a Tool called ‘FitNesseStart’. Set the Command to

$(SolutionDir)Specifications\AcceptanceTests\startfitnesse.bat

set the Initial directory to

$(SolutionDir)Specifications\AcceptanceTests

and check the Use Output window checkbox.

The results should look like this



Opening FitNesse in the Browser
You need only start FitNesse once when you load your project. This starts the FitNesse server running. You seperately navigate to the FitNesse wiki using your browser.

You can start and stop your browser as often as you like, the server will keep running in the background.

We’ll set up another External tool which will load the Wiki on the proper page.

In the External Tools Dialog, Add a Tool called ‘FitNesseBrowser’. Set the Command to

Explorer.exe

set the Arguments to


http://localhost:9092/AcceptanceTests

The results should look like this



Stopping FitNesse
As mentioned above, the FitNesse server will continue happily running away in the background even when you start and stop your browser. Visual Studio External Tools provides a mechanism for shutting down tools, however it doesn’t work for FitNesse.

For now the only solution I’ve found is a separate Tool.

The sample Solution includes another batch file that closes FitNesse.

In the External Tools Dialog, Add a Tool called ‘FitNesseStop’. Set the Command to

$(SolutionDir)Specifications\AcceptanceTests\stopfitnesse.bat

set the Initial directory to

$(SolutionDir)Specifications\AcceptanceTests

The results should look like this



Here’s a curious little thing to note. If you look in the Solution Folder you’ll notice that the Calculator.Specifications project is in a folder called ‘Specifications’, and not ‘Calculator.Specifications’.

This is to make the configuration of the External Tools more generic. It means that when you load a different solution, the external tools will Start and Stop that Solutions own instance of FitNesse, as long as they too are kept in a folder called ‘Specifications’.

The first run
The first time you start FitNesse, the jar file will be extracted to create the Wiki. You saw this in the earlier post. Once everything is extracted the FitNesse server starts running. It all looks like this



First view of the Wiki
With FitNesse running, it’s time to take a look at our Wiki. Select the FitNesseBrowser tool from the Tools menu. What you see should look something like this



Not what you were expecting? Were you thinking you’d see the FitNesse Front Page?

What we’ve done here is make the Tool jump to a specific page in the Wiki, and not going to the regular Front Page. This avoids the need to navigate to our tests every time we start the Wiki.

Since we haven’t created this page yet we’re asked to edit it. So, let’s do that, copy and paste the following code, overwriting the existing contents of the page.

!path bin\Calculator.Specifications.dll

!|import |
|Calculator.Specifications.Fixtures|

This should do simple arithmetic

!|Do Simple Maths |
|A |B |Op|Result?|
|1.0|2.0|+ |3.0 |

Save those changes, but we’re not done with this page yet. Click on the Properties button and change the Page type to ‘Test’. Save the properties and you’ll now see that all important Test Button.



You can click it if you like but it won’t work yet. We have one more piece of setup to do first.

In your Browser, navigate to the URL


http://localhost:9092/root

Edit the ‘root’ page and overwrite the contents with the following

!define TEST_SYSTEM {slim}
!define COMMAND_PATTERN {%m -r fitSharp.Slim.Service.Runner,bin\fitsharp.dll %p}
!define TEST_RUNNER {bin\Runner.exe}

This tells FitNesse to use SLiM which is one of the two protocols that FitNesse can use for linking tests to code. We’re running the implementation of SLiM provided by fitsharp which lets us work with .Net assemblies.

Yes, we could have inserted this bit of configuration at the top of every Test Page, but by putting it in root we can forget about it and stop cluttering up our tests.

Save this page and return to


http://localhost:9092/AcceptanceTests

Now when you click Test, it should work. We have a Test that runs. Admittedly it’s a failing Test, but that’s what we want as a first step.



To get this test to pass we need to look at our code. Our Calculator class throws NotImplementedExceptions rather than implement the actual required logic.

public class Calculator
{
public static decimal Add(decimal a, decimal b)
{
throw new NotImplementedException("Add");
}

public static decimal Subtract(decimal a, decimal b)
{
throw new NotImplementedException("Subtract");
}

public static decimal Multiply(decimal a, decimal b)
{
throw new NotImplementedException("Multiply");
}

public static decimal Divide(decimal a, decimal b)
{
throw new NotImplementedException("Divide");
}
}

Let’s fix that Add method, rebuild and retest.

public static decimal Add(decimal a, decimal b)
{
return a + b;
}



If you’ve made it this far you now have a Solution that has a project under test and a project that has the tests themselves along with all the binaries needed to run FitNesse and use it to test .Net code.

We’ve set up some External Tools in Visual Studio that will make life a little easier.

If you’re a little unsure about how this all hangs together, don’t worry. Over the remaining posts in this series I’ll did a little deeper into the things you can do with FitNesse, and along the way you’ll learn what all the parts of the puzzle are. All posts will use this Solution structure as a basis.

Switch to our mobile site