Using FitNesse’s SLIM protocol to write functional tests in Python

I was searching the internet to look for the tutorial that would help me get started with writing FitNesse tests using Python. TO my dismay there weren’t many that made sense to me. So after 3 days of struggle to find some good resources, I finally managed to kick some FitNesse!

Requirements:

  • Python 2.6 +
  • Setuptools — easy_install: This would help us to install the wrapper for SLIM, waferslim.
  • waferslim: You can get the last updated version of waferslim either from cheese shop or launchpad.
  • fitnesse: Download the fitness standalone from fitnesse’s official website.

Once we have all these, let’s go ahead and create our first fitness page.

Let us organise our directories like this.

/Users/ 
|----redmond/
|----fitnesseTests/
|----Calc/
|----fitnesse-standalone.jar

Once the directories are organised, let’s start to perform the setup.

mkdir ~/fitnesseTests 
cd ~/fitnesseTests 
mkdir Calc

Before creating the fitness page, let’s install waferslim using setuptools.

wget http://pypi.python.org/packages/2.6/w/waferslim/waferslim-1.0.2-py2.6.egg

Now we have waferslim egg in the fitnesseTests directory.

Using easy_install let us install it.

python http:///usr/local/lib/python2.7/site-packages/easy_install.py waferslim-1.0.2-py2.6

This will install waferslim in the current python environment.

Once waferslim is installed it is time to start editing the wiki pages.

Start the fitness server

java -jar fitnesse-standalone.jar -v -p 8080

A long trail of startup code should be visible now.

Fire up a browser and go to http://localhost:8080, the FitNesse front page should be displayed.

Click on Edit and add a wiki word like Calc and save the page.

The Calc will have a question mark after it suggesting us to create a page.

Click on the question mark, a new page’s editor will open.

Fill in the page using the code below

!1 This is a demo for fitnesse 
!2 Use this page as a reference.
!3 Variable Setup 
!define TEST_SYSTEM {slim}
!define SLIM_VERSION {0.1}
!path /Users/redmond/fitnesseTests/
!define COMMAND_PATTERN {python -m waferslim.server --syspath %p}
!3 Imports 
|import |
|waferslim.examples.decision_table|
|Calc.Calc |
|Calc | 
|A|B|multiply?|
|1|2|2 |
|1|0|0 |
|3|5|15 |
|5|5|26 |

Once this part is done, you can click on Test to execute the tests on this page, but it will show you an error.

To link it to our python fixtures we need to write some pythonic code that will execute behind the scene and validate our test.

As we have to multiply two numbers and obtain its results, we need to create a class Calc in Calc.py that will reside in Calc/

cd Calc 
touch __init__.py 
touch Calc.py

Using your favorite editor type in the following code in Calc.py and save it.

from waferslim.converters import convert_arg, convert_result, StrConverter 
_STR_CONVERTER = StrConverter() 
class Calc(object): 
def __init__(self):
self._A = 0
self._B = 0

@convert_arg(to_type=int)
def setA(self, A):
self._A = A
    @convert_arg(to_type=int) 
def setB(self, B):
self._B = B
    @convert_result(using=_STR_CONVERTER) 
def multiply(self):
return self._A * self._B
    def execute(self): 
print "Execute function"
    def reset(self): 
print "Reset function"

Once this is saved, go back to the test page and click on Test.

This time the test should connect to our class and execute it.

For more information and syntax, refer:

[1] Test driven development blog @ http://withaherring.blogspot.fr/

[2] Waferslim’s launchpad page @ https://launchpad.net/waferslim/

[3] FitNesse tutorials @ http://fitnesse.org/FitNesse.FullReferenceGuide/


Originally published at datqaguy.wordpress.com on August 18, 2015.