Wednesday, October 14, 2009

How-to: Use Cucumber with .NET and C# under IronRuby

I will not go into how cool Cucumber is and all that because it is already established. If you have not experienced it, I think you should. It will make a big difference in how you look at code, testing and TDD.
I started using Cucumber on Ruby on Rails projects and I wanted to use it on .NET/C# projects. So, I will in very clear steps show you how to get Cucumber going for your C# projects. This post is just a primer to get your feet wet, but I will come back with follow up posts that will show a more complete project. So, here goes.
Step 1: Setup Ruby (MRI)

  • Download Ruby
  • Install Ruby (MRI version)
  • Check version of ruby and rake
    • ruby -–version
    • rake -–version
  • Update RubyGems
    • gem update --system
Step 2: Setup Cucumber and RSpec using MRI gem
  • Install cucumber gem
    • gem install cucumber
  • Install rspec gem
    • gem install rspec
Step 3: Setup IronRuby
  • Download IronRuby
  • Install IronRuby. I had to just unzip the file I downloaded into C:\IronRuby folder. You can pick any folder or choose to build IronRuby from source.
Step 4: Create a Cucumber wrapper for IronRuby
  • We need to create a script that will execute Cucumber under IronRuby very similar to cucumber.bat in your MRI Ruby folder. I have my MRI Ruby installed at C:\ruby.
  • Create a new icucumber.bat file in your MRI Ruby folder with the following contents:
    • @ECHO OFF
      REM This is to tell IronRuby where to find gems.
      SET GEM_PATH=c:\ruby\lib\ruby\gems\1.8
      @"C:\ironruby\build\debug\ir.exe" "c:\ruby\bin\cucumber" %* 
  • Check to see if Cucumber under IronRuby works
    • icucumber –help
    • you should see something as below. The output has been cut to preserve space.
    • Usage: cucumber [options] [ [FILE|DIR|URL][:LINE[:LINE]*] ]+ cucumber examples/i18n/en/features cucumber --language it examples/i18n/it/features/somma।feature:6:98:113 cucumber -s -i http://rubyurl.com/eeCl
    Step 5: Checkout some examples
    • In a command prompt window, go to the cucumber gem folder under your MRI Ruby installation. The folder path might look like C:\ruby\lib\ruby\gems\1.8\gems\cucumber-<version>\examples
    • Now, run a Ruby example:
      • cd i18n\en
        icucumber features
    • Now, for the litmus test, run a C# example: (See section at the end for updates)
    • cd cs
      compile.bat
      icucumber features
      
    You should see a lot of output scroll by and at the end you should see:
    3 scenarios (3 passed)
    12 steps (12 passed)
    0m0.493s


    Bliss! You have Cucumber running under IronRuby and running Cukes (that is what cucumber features are called) for your C# .NET code.

    Feedback: I would like to hear from you about your experience running Cucumber for your C# projects.

    Update: Missing cs examples
    As of version 0.4.3 of Cucumber, the examples\cs folder is no longer there in the code downloaded with the gem. If you like to run the above code in the post, you can download the zip file for the code at tag 0.4.2 from here. From the top, you can download the zip by clicking on the "Download Source". Then you can copy the "cs" folder to your gem installation of your current cucumber version.

    Update: Get color coded display
    To get a nice color coded display on your windows command line, execute the following:
    igem install iron-term-ansicolor


    Resources
    IronRuby/Ruby Links
    IronRuby website
    IronRuby Wiki
    Ruby website
    Cucumber Screencasts
    http://railscasts.com/episodes/155-beginning-with-cucumber
    http://railscasts.com/episodes/159-more-on-cucumber
    Cucumber Presentations

    12 comments:

    1. Thank you for your detailed post!

      I'm only having one difficulty in following your steps: neither my Ruby nor my IronRuby installs have a "cs" folder under "examples". It's not in "i18n\en", nor in "i18n", nor in "examples".

      Any idea where I can find some Cucumber C# examples?

      Thank you!

      ReplyDelete
    2. Hi brad,
      I noticed the issue you mentioned and I had posted an update. Please see my update section at the end of the post. Hope it helps.

      ReplyDelete
    3. Gotcha -- thanks again!

      ReplyDelete
    4. I hadn't heard of Cucumber before, so I'll check it out. Thanks.

      ReplyDelete
    5. Rats, it doesn't work! First, can't find any version called "MRI" Ruby from the links you give. Second, when running the icucumber.bat file you describe, the -help command says assembly gherkin not found and suggests SET MONO_PATH. Can you help? Thanks! (Oct 18, 2010)

      ReplyDelete
    6. I'm not able to execute examples. I have copied cs folder in to 118n. but when i got error message as
      "C:\Ruby\lib\ruby\gems\1.8\gems\cucumber-0.9.2\examples\i18n\cs>csc /target:libra
      ry /out:Calculator.dll src\demo\Calculator.cs
      'csc' is not recognized as an internal or external command,
      operable program or batch file."

      Please advice me how to slove this.

      ReplyDelete
    7. Hi Bhumi,
      csc, is the C# compiler that comes with Visual Studio. You can download Visual Studio 2010 Express edition from http://www.microsoft.com/express/Downloads/. That should give you the csc executable. Thanks again for reading my blog.

      Thanks,
      Rupak

      ReplyDelete
    8. Hi Figgy,
      The MRI Ruby link is http://www.ruby-lang.org/en/. You will see a big Download Ruby button on the top right.
      I have only tested the code in the article with .NET and not with Mono. I will test it and let you know what I find. Thanks again for reading my blog.

      Thanks,
      Rupak

      ReplyDelete
    9. I am facing an issue while executing cs features. I have install following components

      - rubyinstaller-1.9.2-p0.exe
      - ironruby-1.0v4.msi
      - cs folder

      while running command
      ..\cs>icucumber features

      system gives message:
      C:/IronRuby/lib/ruby/site_ruby/1.8/rubygems.rb:827:in `report_activate_error': C
      ould not find RubyGem cucumber (>= 0)\n (Gem::LoadError)
      from C:/IronRuby/lib/ruby/site_ruby/1.8/rubygems.rb:261:in `activate'
      from C:/IronRuby/lib/ruby/site_ruby/1.8/rubygems.rb:68:in `gem'
      from c:/ruby/bin/cucumber:18

      ReplyDelete
    10. I am facing an issue while executing cs (.Net) example

      C:\Ruby\lib\ruby\gems\1.9.1\gems\cucumber-0.9.3\examples\cs>icucumber features
      C:/IronRuby/lib/ruby/site_ruby/1.8/rubygems.rb:827:in `report_activate_error': C
      ould not find RubyGem cucumber (>= 0)\n (Gem::LoadError)
      from C:/IronRuby/lib/ruby/site_ruby/1.8/rubygems.rb:261:in `activate'
      from C:/IronRuby/lib/ruby/site_ruby/1.8/rubygems.rb:68:in `gem'
      from c:/ruby/bin/cucumber:18

      I installed following components
      - rubyinstaller-1.9.2-p0.exe (Ruby)
      - ironruby-1.0v4.msi (IronRuby)
      - CS folder

      Am i missing something?

      ReplyDelete
    11. Hi Rupak,

      Thank you for reply..

      Is cucumber support ETL (Data warehouse) testing

      Can you please suggest some site/.ppt/yours other blog through which I can understand cucumber testing concepts/process

      Regards,
      Bhumi

      ReplyDelete
    12. Hi Rupak,

      The "icucumber -help" step does not work for me. I get the following error:

      C:\Ruby192\bin>icucumber -help
      C:/Ruby192/lib/ruby/gems/1.9.1/gems/gherkin-2.2.9-x86-mingw32/lib/gherkin/native
      /ikvm.rb:16:in `load_assembly': Assembly 'gherkin' not found\nTry this: SET MONO
      _PATH=C:/Ruby192/lib/ruby/gems/1.9.1/gems/gherkin-2.2.9-x86-mingw32/lib (or expo
      rt MONO_PATH=...) (LoadError)
      from C:/Ruby192/lib/ruby/gems/1.9.1/gems/gherkin-2.2.9-x86-mingw32/lib/g
      herkin/native/ikvm.rb:16:in `native_impl'
      from C:/Ruby192/lib/ruby/gems/1.9.1/gems/gherkin-2.2.9-x86-mingw32/lib/g
      herkin/i18n.rb:7
      from C:/IronRuby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
      `require'
      from C:/IronRuby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
      `require'
      from C:/Ruby192/lib/ruby/gems/1.9.1/gems/gherkin-2.2.9-x86-mingw32/lib/g
      herkin/lexer/i18n_lexer.rb:1
      from C:/IronRuby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
      `require'
      from C:/IronRuby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
      `require'
      from C:/Ruby192/lib/ruby/gems/1.9.1/gems/gherkin-2.2.9-x86-mingw32/lib/g
      herkin.rb:1
      from C:/IronRuby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
      `require'
      from C:/IronRuby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
      `require'
      from C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-0.9.4/bin/../lib/cucum
      ber/cli/main.rb:5
      from C:/IronRuby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
      `require'
      from C:/IronRuby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
      `require'
      from C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-0.9.4/bin/cucumber:5
      from C:/Ruby192/bin/cucumber:19:in `load'
      from C:/Ruby192/bin/cucumber:19


      I have verified that ironruby and cucumber work independantly.


      -Oliseh

      ReplyDelete

    Google