The Fixture Gallery is also available as a PDF document and a live FitNesse wiki. See http://gojko.net/fitnesse/fixturegallery for more information.
Previous page: CalculateFixture Next page: SequenceFixture Parent page: FitLibrary Fixtures

DoFixture

DoFixture can be 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 (see Flow Mode) and wrapping domain objects (see System under test).

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. If the method is void or returns something other than a boolean value, then it is just executed without any effect on the outcome of the test unless an exception is thrown.

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

Fixture class

The fixture class should extend fitlibrary.DoFixture. Declare public methods for all verifications and actions by joining the even cells to get the method name and using odd cells as arguments. You do not have to type the method names directly, just write the table first, then run the test to make it fail for the first time and copy expected method names from the test report.

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(int count,char c){
		char[] arr=new char[count];
		Arrays.fill(arr,c);
		letters=new String(arr);
	}
	public boolean charAtIs(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);
	}	
}

.NET Source Code

using System;
using System.Collections.Generic;
using System.Text;

namespace info.fitnesse.fixturegallery
{
    public class DoFixtureTest : fitlibrary.DoFixture
    {
        private String contents;
        public void FillTimesWith(int howmany, String what)
        {
            contents = "";
            for (int i = 0; i < howmany; i++)
            {
                contents = contents + what;
            }
        }
        public bool CharAtIs(int index, char c)
        {
            return contents[index]==c;
        }
        public void SetList(String[] strings)
        {
            contents = "";
            foreach (String s in strings)
            {
                contents = contents + s;
            }
        }
        //
        public char CharAt(int index)
        {
            return contents[index];
        }
    }
}

Notes

DoFixture also supports a number of keywords that can be used as a prefix to the method name. If a keyword is used, then the odd cells are used to construct the method name. Even cells (apart from the first one) are used as arguments in that case. Here are some of the commonly used keywords:

  • reject will invert the logic of a test, returning TRUE will make the test fail if the row is prefixed with reject.
  • show will print out the value of a calculation in the test results (similar to an empty cell in ColumnFixture).
  • check allows you to verify results of non-boolean calculations. Prefix the row with check and put the expected value of the calculation on the end of the row, in a new cell.
In the Java implementation of FIT, check and show map directly to JavaBean properties, so you do not need to write the get prefix. However, these keywords cannot be used on public fields. In the .NET implementation, you can use them on fields, properties and methods equally. In addition, you can use the set keyword in .NET to set a field or property value.

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

Usage

Use DoFixture to describe workflow tests or tests that do not follow any particular repetitive structure. DoFixture is very good for coordinating other fixtures (see Flow Mode).

Previous page: CalculateFixture Next page: SequenceFixture Parent page: FitLibrary Fixtures


Personal Tools