Insights and outlooks on software development


On getting started using Cucumber for .NET

Wednesday, March 11, 2009 by Thomas L

NOTE: This post is obsolete, please check my new write-up on Cucumber running under IronRuby 0.9.

I'm gearing up for an ALT.NET Oresund meeting in Copenhagen, (March 24 @ IT Universitetet, near the Metro station Islands Brygge). As I've promised to demonstrate running Cucumber stories against .NET code, I needed to get everything running on my lab computer running Vista. Since I heard there had been problems setting up Cucumber, I thought I should document my progress. These are my notes.

The text below is largely based on the IronRuby and .NET page on the cucumber wiki, but I had to jump though some hoops to get things working.

Installing MRI

Since IronRuby isn't exactly 1.0 yet, we need the Matz's Ruby Interpreter (often referred to as MRI) to get stuff working. Since we're running on Windows, we can install the Ruby One-click installer from RubyForge.

When installing, make sure that you check the checkbox for RubyGems. I checked all of the checkboxes, just to be sure. Place MRI in C:\External\languages\ruby\ruby-1.8.6.

When MRI is installed, you can invoke ruby --version and gem --version. Both should work, giving the versions of the two tools.

Installing IronRuby

IronRuby is, if you didn't know, a Microsoft-backed open-source interpreter on the CLI, using the .NET dynamic runtime to execute Ruby code in the .NET world, making it possible to interop with regular .NET assemblies. The interop feature is the thing we are looking for, since we want to get Cucumber running stories against .NET code written in e.g. c#.

To install, download IronRuby. At the time of writing, the latest version was IronRuby 0.2 Alpha, packaged in a zip file. The zip file contents should be placed in C:\ironruby, and then the path to IronRuby's bin folder (C:\ironruby\bin) should be added to the PATH. If everything is fine, it's possible to invoke ir.exe which launches the IronRuby console.

Getting Cucumber and RSpec

Now we have both MRI and IronRuby working, it's time to direct the attention to Cucumber and RSpec. To install this, we use RubyGems to automatically download and install the plugins (called 'Gems') by invoking gem install cucumber rspec.

Now the cucumber and rspec gems should be installed. The Cucumber gem should have placed a file called cucumber.bat underneath the MRI installation directory. To make sure everything works as it should using the MRI, start a cmd prompt and run cucumber. If everything is set up correctly, there should be an error about a missing cucumber.yml. We have now gotten cucumber running under the MRI.

Now for the next step; getting cucumber running under IronRuby.

Cucumber wrapper script

Create a bat file called icucumber.bat in the MRI bin directory that should wrap the cucumber.bat. On my system, having Ruby and IronRuby where I placed them, this is the contents for me:

REM This is to tell IronRuby where to find gems.
SET GEM_PATH="C:\External\languages\ruby\ruby-1.8.6\lib\ruby\gems\1.8"
@"C:\ironruby\bin\ir.exe" "C:\External\languages\ruby\ruby-1.8.6\bin\cucumber" %*

To test if this works, run icucumber, and you should get the same error as previously, the one about a missing cucumber.yml file.

This was where things didn't work as it should for me. icucumber quit, shouting about not being able to load the cucumber gem, and somehow it was looking underneath {MRI}\lib\ironruby\gems\1.8, even though my GEM_PATH was set to the above. My solution to that problem was to copy everything from the ruby gem folder to the IronRuby gem folder. I thus have to remember to copy things from the ruby to the ironruby folder when anything is updated, but it at least kind of works.

Testing if icucumber works as it should

Earlier we only tested if we could start icucumber. The really interesting thing is to check if icucumber can run stories for code written in c#.

cd to the cucumber gem directory; in our case this would be c:\External\languages\ruby\ruby-1.8.6\lib\ironruby\gems\1.8\gems\cucumber-0.1.16\examples

To test if we can run icucumber stories testing plain ruby code, do the following.
cd c:\External\languages\ruby\ruby-1.8.6\lib\ironruby\gems\1.8\gems\cucumber-0.1.16\examples\i18n\en
icucumber features

To test to run icucumber stories for c# code: If you haven't already, run a Visual Studio 2008 command prompt (to get the paths to the .NET Framework). Then do the following.
cd C:\External\languages\ruby\ruby-1.8.6\lib\ruby\gems\1.8\gems\cucumber-0.1.16\examples\cs
icucumber features

The command above should crank out "20 steps passed", which means we have gotten our Ruby-to-.NET interop running!

But why?

This is the most interesting question of all. I believe that the BDD way of writing executable specs is something that we as software developing teams should start doing right away. It helps on many levels; not only will our test code be more readable, it actually helps when fleshing out how the features will work. Us as teams will gather and write the specs in free-text and the developers will then fill in the gaps (i.e. write the steps), instead of having specs on one place and unit/integration/acceptance tests on the other.

If you come to the ALT.NET Oresund gathering March 24, I am more than interested in discussing this topic. I'll hopefully be able to show a demonstration of how BDD with Cucumber works tech-wise and then we can take the theory and the why afterwards.

6 kommentarer:

zvolkov said...

But it does not support ANSI colors!

Thomas L said...

True, there is no fancy coloring in the console, like you'd get when using cucumber in the regular MRI (after installing the gem win32console).
A bigger problem is perhaps that the performance seems to be quite bad, at least on my dev/lab machine, running Vista under VMware Fusion on a Macbook 2GHz.

Chris Kooken said...
This comment has been removed by the author.
Chris Kooken said...

Nice article. Here is an article I wrote about debugging Cucumber in .net using Ruby in Steel.

Tom Janssens said...
This comment has been removed by the author.
Tom Janssens said...

Hi there,

Just thought I would let you know about a possible alternative for cucumber in .Net; I'm currently in the early stages of developing it; your opinion would be appreciated; you can find the article here: Aubergine