$ cat "

Getting Started with FitNesse

"

If you haven't heard of FitNesse, it is Unclebob's acceptance testing framework which let's you specify test cases as wiki pages. These test cases then interact with fixture classes that call the system under test.

The first step in getting started with FitNesse is to download it. You only need one file, FitNesse.jar, which can be found at the FitNesse wiki.

Download fitnesse.jar and put it in your project's lib directory. For the purpose of this blog post we will assume a directory structure that looks like this:

c:- code
- hellofitnesse
- bin
- fitnesse
- lib
- src

So, now the lib folder contains fitnesse.jar.

Since FitNesse is based on a wiki, it runs on its own web server. To start fitnesse go to the hellofitnesse directory and execute the following command:


java -jar lib/fitnesse.jar -p 8080 -r fitnesse


This command starts fitnesse and tells it to listen to the port 8080. It also tells it to unpack all the files that it needs in the folder fitnesse.

You can now open up a browser and go to localhost:8080. You should then see the default FitNesse wiki.

Edit the front page by clicking the edit link in the menu to the left and add the following line:

!define TEST_SYSTEM {slim}

!path lib/fitnesse.jar
!path bin

>HelloFitnesse

The first line tells FitNesse to use slim to run the tests. The path statements tell FitNesse where to find fitnesse.jar and the .class files for your fixtures (change this path if your .class files will be generated into some other directory).

This last line creates a link to a child page called HelloFitnesse. When you save the page, a little question mark will appear after HelloFittnesse. Click the question mark to create the page.

This page is now just an ordinary wiki page. To make the page into a test suite page click the properties link in the menu, and select Suite.

Edit the HelloFitnesse page and add the following row:

>MyFirstTest

Create the page and change its properties to make it into a test page.

Edit the MyFirstTest page and add the following code to create a simple test:

|import  |
|fixtures|

!|AddFixture |
|a |b |sum? |
|1 |2 |3 |
|2 |2 |3 |

The first two lines defines a table with all the packages to import. The second table defines the actual test. The first row specifies the name of the fixture class to run. The columns a and b represent state that should be set in the fixture, and the column sum? represents a method call which returns the result that should be verified.

The values a and b are set by calling the methods setA and setB with the corresponding values. When both a and b are set the method sum() will be called and the return value will be compared to the expected value.

You can now run the test by clicking the Test link in the menu. However, since the actual test fixture has not been written yet you will get an exception that looks something like this:

Could not invoke constructor for AddFixture[0]

So, the next step is to write the fixture class.

package fixtures;

public class AddFixture {
private int _a;
private int _b;

public void setA(int a) {
_a = a;
}

public void setB(int b) {
_b = b;
}

public int sum() {
return _a + _b;
}
}

Compile the fixture class and make sure the AddFixture.class file is actually located in bin/fixtures/ folder (if bin is your output folder, specified in the !path directive earlier). You should now be able to run the test and get the proper response, one row should succeed and one should fail.

Written by Erik Öjebo 2010-04-16 22:05

    Comments