Using fixtures from within a SequenceFixture
June 12, 2006 by Isaac
The FIT SequenceFixture is essentially the same as a DoFixture. I just like the way commands are written better. One of the cool things about a SequenceFixture (or DoFixture) is that it allows you to use other fixture types from within. So, you could have a RowFixture running some tests then decide that a ColumnFixture would work better for the next step, except for one detail, you need some of the private variables that live inside the RowFixture. Using a SequenceFixture that's not a problem.
See, ordinarily, a fixture persists only as long as the table (well, that's almost true, but we won't get into that). So, to pass data between fixture types you could use a setup table, which I used to do before figuring out SequenceFixtures. That method is painful, but works. DoFixtures and SequenceFixtures solve that by allowing a fixture to be returned, instead of a simple string or number. If you return a fixture to a DoFixture it will execute that fixture. Nice! This also allows you to pass in variables that have been saved in the "master" class.
Ok, on to the examples. In FitNesse, suppose you define some tables like this:
!define COMMAND_PATTERN {python %m %p}
!define TEST_RUNNER {/usr/bin/FitServer.py}
We're testing a sequence fixture here. The sequence fixture allows us the cool capability to use other fixtures inside. For example, we can use a ColumnFixture or RowFixture from inside a SequenceFixture.
The declaration of a sequence fixture must be the first on the page.
|fixtureTests.sequenceTest|
Use some sequence fixture calls to shove some data into the class.
|add user|Some Person|
|add user|Someone Else|
|add user|Joe Somebody|
|add user|Jane Doe|
We use a RowFixture here to pull all the data out of the SequenceFixture and run a comparison. The RowFixture let's us do the comparison on an unordered list.
|get user|
|name|
|Someone Else|
|Jane Doe|
|Some Person|
|Joe Somebody|
Test a column fixture.
|test column|
|str|sout?|
|Hello|Hello|
|Hi there|Hi there|
First, we use the SequenceFixture to shove some data into a variable. Then we use a RowFixture to pull it out and do a comparison. Finally, we use a ColumnFixture just for fun. The Python code for this is:
from fitLib.SequenceFixture import SequenceFixture from fit.RowFixture import RowFixture from fit.ColumnFixture import ColumnFixture class Wrapper(object): def __init__(self, name): self.name = name def __eq__(self, other): return self.name == other def __ne__(self,other): return self.name != other def __str__(self): return self.name class colTest(ColumnFixture): _typeDict = {"str": "String", "sout": "String" } str = "" def sout(self): return self.str class sequenceTest(SequenceFixture): _typeDict={} _users = [] def __init__(self): self._users=[] super(sequenceTest,self).__init__() _typeDict['addUser.types'] = ['Boolean', 'String'] def addUser(self, name): self._users.append(Wrapper(name)) return True _typeDict['getUser.types'] = [None] rfTypes = {"name": "String"} def getUser(self): return RowFixture(self._users, self.rfTypes) _typeDict['testColumn.types'] = [None] def testColumn(self): c = colTest() return c
I'm not really fond of the way RowFixtures work from within a DoFixture, and I never would have figured out all that mangled junk on my own. Thank goodness for the FitNesse mailing list and that the author of PyFIT is so responsive. Essentially, what has to happen is that the string needs to be wrapped in an object so that our mangled RowFixture knows what to do with it. I don't confess to understand exactly why this is needed, but according to the PyFIT author it is, so I suppose I should trust him. ![]()
Thankfully, using a ColumnFixture is more intuitive. Just declare a ColumnFixture as normal and then return the whole fixture. Simple.
Posted in 
content rss
