≡ Menu

Fitnesse and Ruby – a basic tutorial

A couple of weeks ago I posted a tutorial for getting Fitnesse and .NET working together. It was a good success, but I had an ulterior motive. I really just wanted to suck you, the reader, in so I could bring to you Ruby – a fun OO language.

However, getting Ruby hooked up proved to be a little trickier than I expected. Partly because I’m not all that well-versed in Ruby yet, and partly because there is even less documentation on the net for Ruby and Fitnesse. But, nonetheless, I’ve got my Ruby tests working, so lets get down to business!

Step 1 – Install Ruby

First things first, you’ll need to get and install Ruby. I created this tutorial on my Linux box, so I just emerge Ruby‘d it. However, you can just as easily download it and install it.

Step 2 – Install Fitnesse (and Ruby FIT)

If you haven’t installed Fitnesse, grab it and install it. Once you have it installed, grab the ruby.zip file from here and unzip it to your Fitnesse install directory.

(Note: There are other versions of Ruby FIT around. I chose to use the one off the Fitnesse site. However, you may want to check out the discussions and choose for yourself. )

Finally, start up Fitnesse and browse to it. So far, so good.

Step 3 – Setup our test

Let’s get down to business. Create a wiki page called MyRubyTest. Similar to the .NET runner, we have to specify a different command pattern. Setup your page so it looks like:

!define COMMAND_PATTERN {ruby -I %p ruby/bin/FitServer.rb -v}

!|mytest.Division|
|numerator|denominator|quotient?|
|10       |2          |5        |
|12.6     |3          |4.2      |
|100      |4          |24       |

Now save it, and set the properties to be a Test. Run the test and you should get something like:

Which means we are ready to dive into some Ruby!

Step 4 – Build our class

This step was the big stumbling block for me. The class was easy to write. It was getting the Ruby Fit runner to find it that was the hard part. The trick is that Fit wants your class to be in a directory path and module that sync up. Let’s look at the code and I’ll explain more.

Create a directory somewhere called mytest. I chose ~/apps/mytest, but for windows, use c:\projects\mytest or whatever you prefer. In that directory, create a Ruby file called division.rb. You can do this with notepad, or use the FreeRIDE IDE that comes with the Ruby installer for Windows, or the good ol standby, vi ;) The contents should look like:

require 'fit/column_fixture'

module Mytest

  class Division < Fit::ColumnFixture
    attr_accessor :numerator, :denominator
    def quotient; @numerator.to_f / @denominator.to_f; end
  end

end

(which I got mostly from the division.rb file in the lib/eg directory).

So, now you have a file, division.rb that is in a directory called mytest. The class definition for Division is in a module called Mytest. This is the key to getting Fit to recognize your classes. Or so it seems – I’m always open to corrections if someone has different information.

Step 5 – Hook up the class to Fitnesse

So, now we have a failing Fitnesse test, and a class that we think will make it pass. Let’s edit our page and add the path to our class. Open up MyRubyTest for Edit, and add the path definition, modified for the path you chose above, just below our COMMAND_PATTERN. Your test should now start with

!define COMMAND_PATTERN {ruby -I %p ruby/bin/FitServer.rb -v}
!path ~/apps/

(Or !path C:\projects. You want to go one directory below the directory your class is in).

Save it and run it. You should now get:

Great! We’ve managed to get Fitnesse talking to our Ruby class. Now, modify our test so that we are expecting the right values (change the 24 for the 100 test to 25) and run the test again. You should get green!

And just like that, you’ve got Fitnesse talking to Ruby. You deserve a hardy pat on the back. Now you can get cracking with your customer tests not letting any language stand in your way. As long as that language is Java, .NET, Ruby, Python, or one of the other supported servers.

Have fun!

Comments on this entry are closed.

  • Anonymous December 9, 2005, 3:31 pm

    Hi Cory,

    nice little introduction to run RubyFIT on Fitnesse. I’d like to tackle some of the issues you talked about, to help shed some light on them.

    First: the RubyFIT version hosted on the Fitnesse wiki does not contain the latest code, which is hosted on the appropriate project page on RubyForge. If you should encounter limitations or other issues, please consider upgrading to the latest code (unfortunately still available through CVS only) before reporting a bug.

    About the issue on making RubyFIT finding the fixture class you found confusing: you got it right. That design choice is unfortunately described down in a comment on the fixture_loader.rb file:

    Try to load the named class. [Technical details you’re not really interested in…] This means that the class Example::Sqrt must be in the file Example/sqrt.rb or the file example/sqrt.rb.

    I’ll consider stating this detail much more clearer in the README included in the distribution. Sorry about that much hassle I’ve caused.

    — Giulio Piancastelli

  • Mike May 20, 2008, 2:42 am

    Great tutorial! What happens if FitNesse doesn’t actually display the exceptions? My test summary says there are two exceptions but then doesn’t print them out like in the tutorial screenshot. I didn’t see any logging level options in the FitNesse startup parameters.

    Thanks,

    Mike

  • jbonnet November 24, 2008, 3:45 am

    Hi!
    I know this is a rather ‘old’ post, but the truth is… it still appears on top of all the searches…

    It seems that there is a too big step (at least for me) when you write ‘Finnaly, start up Fitness and browse to it’…

    Do I have to run FitServer.rb from where gems has installed it?

    Thanks,
    jb

  • Cory Foy November 24, 2008, 7:58 am

    Hi jbonnet,

    Almost. What you’ll want to do is make sure you have Java installed, then run either run.bat or run.sh from the Fitnesse install directory. You can pass “-p 8080” to change it to port 8080, or change the number to be whatever port you want to run it on.

    If you are still having problems, give me a shout at foyc at cornet design dot com

  • Cory Foy January 28, 2009, 11:16 pm

    Hi Bobby,

    No, that should be fine. Fitnesse doesn’t actually care where it is – you just have to make sure to set your path variable correctly.

    Cory

  • Bobby Washington January 29, 2009, 9:05 am

    Cory, you were right. I figured out what my issue was. I had

    path ~/apps/

    defined as C:\Projet

    instead of C:\Project

    Works like a charm now. I’m totally stoked. Thanks for this giving back with this wonderful blog.