FitLibrary Fixtures

FitLibrary started as a third-party set of fixtures, but it was so useful that it is now considered as a part of the standard test types. FitLibrary provides general-purpose library fixtures for Fit and FitNesse. It includes Fixtures, which provide an elegant way of organizing story tests in general and of expressing workflow in particular. Let us discuss each of them one by one.

SetUpFixture is an excellent replacement for ColumnFixture. It canbe used to set the stage for other fixtures. For example, it can be SetUpFixture to insert rows into a database table or to create domain objects that will be used in later tests.

Table Format

The first row of a SetUpFixture table is the fixture class name. The second row lists property names for object. All rows after that contain the property values. There are no output cells in a SetUpFixture table, all cells are used for inputs.

!|SetUpFixtureTest|
|player|postcode|balance|
|John Smith|SW4 66Z|10.00|
|Michael Jordan|NE1 8AT|12.00|

Java Source Code

 package info.fitnesse.fixturegallery;  
 import info.fitnesse.fixturegallery.domain.TaxCalculator;  
 import fitnesse.fixtures.TableFixture;  
 public class TableFixtureTest extends TableFixture{  
      protected void doStaticTable(int rows) {  
           TaxCalculatortc=new TaxCalculator();  
         double totaltax = 0;  
         for (int row = 1; row < rows - 3; row++)  
         {  
      totaltax += tc.GetTax(getText(row, 1),  
      Double.parseDouble(getText(row, 2)));  
         }  
         double taxintable = Double.parseDouble(getText(rows - 2, 2));  
         if (taxintable == totaltax)  
          right(rows - 2, 2);  
         else  
          wrong(rows - 2, 2,String.valueOf(totaltax));  
        }  
 }  

Usage

SetUpFixture is used to initialize a list of domain objects, to prepare a database table or to execute a method several times with different arguments.
Do not use the SetUpFixture if the result of the operation or error verification is important. (Use the ColumnFixtureor the CalculateFixture instead)

CalculateFixture is used to verify the result of one or more calculations for different combinations of input arguments. It does the same job as ColumnFixturein a different table format and with a less code in the fixture class.

Table Format

The first row of the table is the fixture class name. After that, the second row contains names for input parameters, followed by an empty cell, then followed by names of calculations (output values). All rows after that specify values for input parameters and expected outcomes for calculations. An empty cell is again used to separate inputs from expected outputs.

!|CalculateFixtureTest|
|firstPart|secondPart||together|
|Hello|World||Hello, World|
|Houston|We Have a Problem||Houston, We Have a Problem|

Java Source Code

 packageinfo.fitnesse.fixturegallery;  
 import fitlibrary.CalculateFixture;  
 public class CalculateFixtureTest extends CalculateFixture{  
      public String togetherFirstPartSecondPart(String firstPart,StringsecondPart){  
           return firstPart+ ", "+secondPart;  
      }  
 }  

Usage

CalculateFixture is usedto execute the same method or a set of methods for different combinations of input arguments and verify the results. If user just wants to execute a method and the result is not important (or the method is void), use the SetUpFixture instead. CalculateFixture treats an empty cell as a blank string, and compares it to the result, so it will fail the test if user leaves an output cell empty.

DoFixture is used to describe story-like tests, almost in plain English. It is a much more efficient replacement for ActionFixture and also has some great features like flow-mode coordination and wrapping domain objects.

Table Format

The first row of the table lists the fixture class name. All rows after that are used to execute verifications or perform actions by executing methods of the fixture class. The method name is constructed by joining odd cells in the row. Argument values are taken from even cells.

If the method returns a Boolean value, the row is considered to be a test and returning FALSE will make the test fail.

!|DoFixtureTest|
|fill|10|times with|x|
|char at|4|is|x|
|set list|A,B,C,D|
|char at|2|is|C|

Java Source Code

 package info.fitnesse.fixturegallery;  
 import java.util.Arrays;  
 import fitlibrary.DoFixture;  
 public class DoFixtureTest extends DoFixture {  
      public String letters;  
      public void fillTimesWith(intcount,char c){  
           char[] arr=new char[count];  
           Arrays.fill(arr,c);  
           letters=new String(arr);  
      }  
      public booleancharAtIs(int position, char c){  
           return letters.charAt(position)==c;  
      }  
      public void setList(char[] array){  
           letters=new String(array);  
      }  
      public char charAt(int position){  
           return letters.charAt(position);  
      }  
 }  

Usage

DoFixture is used to describe workflow tests or tests that do not follow any particular repetitive structure. DoFixture is very good for coordinating other fixtures.

SequenceFixtureis very similar to DoFixture and has almost the same features. In fact the only difference between those two is the naming convention for methods. Instead of using odd cells to construct a method name, SequenceFixturetakes the first cell in each row as the method name, and all other cells as arguments. All DoFixture keywords are supported in SequenceFixturetoo.

Table Format

The table format is the same as for DoFixture with the difference in method naming.

!|SequenceFixtureTest|
|fill|10|x|
|check|char at|4|x|
|set list|A,B,C,D|
|check|char at|2|C|

Java Source Code

 package info.fitnesse.fixturegallery;
 import java.util.Arrays;
 import fitlibrary.SequenceFixture;
 public class SequenceFixtureTest extends SequenceFixture{
      public String letters;
      public void fill(intcount,char c){
           char[] arr=new char[count];
           Arrays.fill(arr,c);
           letters=new String(arr);
      }
      public void setList(char[] array){
           letters=new String(array);
      }
      public char charAt(int position){
           return letters.charAt(position);
      }
 }

Usage

SequenceFixturehas all the flexibility and power of DoFixture. It is most useful for more technical workflow tests.

ArrayFixture was built as a replacement for RowFixture. It works very similar to other fixture types, with two main differences:

  1. Element order is important for ArrayFixture.
  2. ArrayFixture can work with generic collections as well as with arrays.

Table Format

The first row of the table should list the fixture class name. The second row lists the structure of collection elements — names of fields, properties and methods.

!|ArrayFixtureTest|
|name|postcode|creditlimit|
|JohnSmith|SW466Z|10|
|Michael Jordan|NE1 8AT|12|

Java Source Code

 packageinfo.fitnesse.fixturegallery;  
 import info.fitnesse.fixturegallery.domain.Player;  
 import fitlibrary.ArrayFixture;  
 public class ArrayFixtureTest extends ArrayFixture{  
      public ArrayFixtureTest() {  
           setActualCollection(Player.players);  
      }  
 }  

Usage

ArrayFixture is usedwhen the element order is important or to avoid conversion of an object list into an array.

CombinationFixture is used to perform an operation on pairs of values. The values are specified in a row and a column and the operation is performed on all combinations of values.

Table Format

The first row of the table is the fixture class name. The second row contains an empty cell followed by cells containing the values to be used as the second parameter for the operation. All subsequent rows contain a value in the first cell to be used as the first parameter for the operation, followed by cells containing the expected value of the operation.

!|CombinationFixtureTest|
|  |1|2|3|
|6 |6 |3|2|
|12|12|6|4|

Java Source Code

 packageinfo.fitnesse.fixturegallery;  
 import fitlibrary.CombinationFixture;  
 public class CombinationFixtureTest extends CombinationFixture{  
      public int combine(inttheFirst, inttheSecond) {  
           return theFirst / theSecond;  
      }  
 }  

Usage

CombinationFixture is used to describe calculation rules that depend on exactly two arguments.

ConstraintFixture is a variation of CalculateFixture that has an expected value of true for each calculation.

Table Format

The first row of the table is the fixture class name. After that, the second row contains names for input parameters. All rows after that specify values for the input parameters.

!|ConstraintFixtureTest|
|firstPart|secondPart|
|1|2|
|2|3|

Java Source Code

 packageinfo.fitnesse.fixturegallery;  
 import fitlibrary.ConstraintFixture;  
 public class ConstraintFixtureTest extends ConstraintFixture{  
      public booleanfirstPartSecondPart(intfirstPart,intsecondPart){  
           return firstPart<secondPart;  
      }  
 }  

SetFixture is the same as ArrayFixture with one difference:

1. The order of the rows is not checked.

Usage

SetFixture is usedin place of RowFixture in Java when working with JavaBeans objects, because it works correctly with JavaBeans getters. Use it instead of ArrayFixture when the order of elements is not important.

SubsetFixture is a variation of SetFixture with one difference:

1. The rows in the fixture table can be a subset of the actual rows.

Usage

SubsetFixture is usedin place of RowFixture and SetFixture when you want to ignore surplus elements. For example: check whether some rows exist in a database regardless of other rows that might be in the same database table.

Note:

1.      There may be the cases where it would need to include seamless SetUpFixture in the following way just above table.

!include -seamless SetUpFixture

2.      There are some other fixtures like Command line fixture, Html fixture, RowEntry Fixture etc.

This was all about fixtures. Next we would be discussing – Using Fitnesse on Remote Machine.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>