The Fixture Gallery is also available as a PDF document and a live FitNesse wiki. See http://gojko.net/fitnesse/fixturegallery for more information.
Next page: Flow Mode Parent page: Important concepts

Fixture Arguments

The first row of a FIT table is normally used to initialise the fixture class. In addition to that, you can pass arguments to the fixture by appending cells after the class name in that row. Fixture arguments work very similar to command-line arguments of a main method and they can be accessed using the args array in the fixture.

You can use this feature to parameterise your fixtures and make them more reusable. For example, arguments allow you to pass parameters to a RowFixture:


!|ArgumentsTest|Hello World|Houston We Have a Problem|
|word|
|Hello|
|World|
|Houston|
|We|
|Have|
|a|
|Problem|

Java Source Code

package info.fitnesse.fixturegallery;

import java.util.HashSet;
import java.util.Set;

import fitlibrary.SetFixture;
import fitlibrary.parse.Table;

public class ArgumentsTest extends SetFixture{
	public class Word{
		public String word;
		public Word(String w){
			this.word=w;
		}
	}
	public void doTable(Table arg0) {
	  Set<Word> set=new HashSet<Word>();
		for(String s: args){
			for (String word: s.split(" ")) set.add(new Word(word));
		}
		this.setActualCollection(set);
		super.doTable(arg0);
	}	
}

.NET Source Code

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

namespace info.fitnesse.fixturegallery
{
    
    public class ArgumentsTest: fit.RowFixture
    {
        public override Type GetTargetClass()
        {
            return typeof(Text);
        }
        public override object[] Query()
        {
            List<Text> t = new List<Text>();
            foreach (String s in Args)
            {
                foreach (String w in s.Split(new char[] { ' ' }))
                {
                    t.Add(new Text(w));
                }

            }
            return t.ToArray();
        }
    }
}

Python Source Code

# REQUIRE: Python >= 2.4, due to set() usage

from fitLib.SetFixture import SetFixture
import types

class Word(object):
    """Simple ValueObject class to store a word as string."""
    _typeDict = { "word": "String" }

    def __init__(self, word):
        assert isinstance(word, types.StringTypes)
        self.word = word

class ArgumentsTest(SetFixture):

    def getTargetClass(self):
        return Word #< CLASS-HINT: For _typeDict lookup.

    def doTable(self, table):
        wordSet = set()
        for s in self.args:
            for word in s.split(" "):
                wordSet.add( Word(word) )
        # -- WEIRD: setActualCollection() takes no arg -> Preassign first.
        self.paramCollection = wordSet
        self.setActualCollection()
        SetFixture.doTable(self, table)

Smalltalk Source Code

'From VisualWorks®, 7.6 of March 3, 2008 on June 27, 2008 at 3:36:15 pm'!


Info.Fitnesse.Fixturegallery defineClass: #ArgumentsTest
	superclass: #{Fitlibrary.SetFixture}
	indexedType: #none
	private: false
	instanceVariableNames: ''
	classInstanceVariableNames: ''
	imports: ''
	category: ''!

!Info.Fitnesse.Fixturegallery.ArgumentsTest methodsFor: 'enumerating'!

doTableNode: aTableNode
	| set |
	set := Set new.
	arguments
		do:
			[:each | 
			(each tokensBasedOn: Character space)
				do: [:eachWord | set add: (Word word: eachWord)]].
	self actualCollection: set.
	super doTableNode: aTableNode! !
'From VisualWorks®, 7.6 of March 3, 2008 on June 27, 2008 at 3:36:57 pm'!


Info.Fitnesse.Fixturegallery defineClass: #Word
	superclass: #{Core.Object}
	indexedType: #none
	private: false
	instanceVariableNames: 'word '
	classInstanceVariableNames: ''
	imports: ''
	category: ''!

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!


!Info.Fitnesse.Fixturegallery.Word class methodsFor: 'instance creation'!

word: aString
	^self new word: aString! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!


!Info.Fitnesse.Fixturegallery.Word methodsFor: 'type access'!

signatureFor: aSymbol
	^String! !

!Info.Fitnesse.Fixturegallery.Word methodsFor: 'accessing'!

word
	^word!

word: aString
	word := aString! !

Note that arguments are just plain strings. Symbols do not work automatically with arguments, so trying to pass <<symbolname in .NET will not work out-of-the-box. If you want to process arguments like normal cells in .NET, you can use the GetArgumentInput method (see http://syterra.com/FixtureArguments.html).

A very important thing to remember related to arguments is that they will not be available in the fixture constructor, since they are initialised after the class is loaded. They will be available to other methods, however.

Next page: Flow Mode Parent page: Important concepts


Personal Tools