Mar 09 2009
How to implement loops in FitNesse test fixtures
An e-mail came through this morning illustrating one of the most common problems that developers have with FIT/FitNesse and probably the biggest cause of pain for the whole team with acceptance testing. The answer to the problem requires a change in the way people think and is typically the first thing I try to do when I get called in to help a team with FitNesse.
Here’s the e-mail:
I want to implement while and if conditions in fitnesse using java , particularly under DoFixture. Can any body know is it possible to implement conditional loops for DoFixture and something else.
I want to use while condition in following way:
|While|count|<|count| |statement1|statement1| |endLoop|
Theoretically you could do this with a custom fixture that sets the condition in the first table row (dofixture method call) and then returns a fixture which iterates through the rest of the table. But my question here is: why on earth would you do it like this?
Acceptance tests (in this case FitNesse pages) should describe the specification of what the software should do (or does in regression test), not how it the test is performed. Fixtures in the code should implement the script for the test. So, for example, instead of:
|While|i=0|i<5|i++| |put book in the cart|i| |confirm selection|i|
it is much much better to specify this as
|buy|5|books|
and then implement the loop in the buyBooks(int howmany) method in the fixture. The benefits of this approach are:
- The test is easier to read and understand. It is absolutely clear what the FitNesse page specifies and in a wider context it allows you to see important details while not concerning yourself with unimportant stuff. Business people and testers can understand this straight away. Developers will not get distracted by unimportant details. the test will allow you to have a much more meaningful discussion on the specification.
- The test is also easier to maintain in the future. Code refactoring will automatically propagate to fixture code, but not to FitNesse pages, so you want to move all the complexity outside of the pages into fixtures and make acceptance tests less fragile.
Mastering the difference between a specification (what) and a script (how) is probably the single most important thing that teams have to do in order to efficiently implement acceptance testing and really get the most out of it. Without that, tests quickly become a pain to maintain, they are not really understandable by anyone and lose their value as a communication enhancer straight away.
For a more detailed discussion of this, see David Peterson’s Concordion Technique page and key ideas from my latest book Bridging the communication gap.
![]() |
![]() |



This one reminds me on a picture on the solid principles, I just saw lately: http://www.lostechies.com/cfs-filesystemfile.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derickbailey/SingleResponsibilityPrinciple2_5F00_71060858.jpg
Just because you can use FitNesse for every stuff, doesn’t mean you should.
So true! It’s going to be a long trip to get people forget years of scripted test automation and go for tests that reveal user’s intention and not procedural actions and detailed algorithms. Good post.
Great post! Takes me back to my first agile project. I was so used to programming these robust, data-driven test scripts. It was the Canoo WebTest developers who first made the case to me that tests should be specified, not programmed. Shortly after that I realized the power of FitNesse as a collaborative tool to turn examples into executable, yet still readable, tests. Now we have many tools that allow this approach.
A side point: just because you could code complex logic into test scripts, doesn’t mean you should. Our GUI regression test scripts have little logic other than the occasional ‘if’, and they have found regression bugs right away for years now. Our FitNesse tests find regression bugs less often, but their power is in coding the right thing to start with.