Friday, February 8, 2008

Fitnesse

Overview Fitnesse is a wiki. Besides providing the standard features of a wiki, Fitnesse allows tests for Java classes to be created. The tests are created by adding text in a particular format to a webpage. The wiki translates the text that specifies the tests into calls made to classes that extend fit.ColumnFixture. In this example, tests in the form
|daily fine?|grace period?|checkout period?|
|10|3|20|
are translated into calls to
dailyFine()
gracePeriod()
checkoutPeriod()
Fitnesse Fixture Provide the interface between the Fitnesse test that we will be adding to the wiki page and the application code by writing a Fixture.
package fixtures;
import com.frisco.library.*;
import fit.*;

public class BookRules extends ColumnFixture {
   public int dailyFine() {
      return new Book().getCheckoutConstraints().getCentsPerDay();
   }
   
   public int gracePeriod() {
      return new Book().getCheckoutConstraints().getGracePeriod();
   }
   
   public int checkoutPeriod() {
      return new Book().getCheckoutConstraints().getPeriodAsDays();
   }
}
Compile Example Code Create the fixture class and the classes to be tested from the source available from http://www.developer.com/services/article.php/3649506 Install Fitnesse Download fitnesse20070619.zip from http://fitnesse.org/FitNesse.DownLoad and extract. Run Fitnesse Double-click on fitnesse\run.bat

Create a Test in a Fitnesse Wiki Page
1. In your browser, go to http://localhost to get to Fitnesse Frontpage.
2. Create a new testing page by appending the page name to the URL, i.e., http://localhost/LibraryTests
3. You should see a webpage with a banner reading Not Found ... and a small link to Create this Page. Click the link.
4. In the new webpage, add the path to the classes required for the tests.
For me this is - C:\Documents and Settings\GHELTON1\src\fitnesse\Library\bin and add the tests that execute the methods in the fixture class in the form shown below:
!path C:\Documents and Settings\GHELTON1\src\fitnesse\Library\bin

!|fixtures.BookRules| |daily fine?|grace period?|checkout period?| |10|3|21|
and Save.

Modify Properties Click on the Properties link in the left hand panel to edit the page properties. In the Properties dialog, click the Test property and Save.
Test! Click the Test link in the left hand column to perform the tests. The tests that pass will be colored with a green tint and those that fail will be highlighted with a reddish tint.



Conclusion
For more info about tests and options to run them, see http://www.ibm.com/developerworks/java/library/j-cq02286/index.html
Different types of fixtures identified and discussed http://fit.c2.com/wiki.cgi?FieldGuideToFixtures
Ron Jeffries how-to - http://www.xprogramming.com/xpmag/dbcAcceptance.htm