I just started using Cucumber to write acceptance tests for a new command-line application I’m writing. I used the wonderful newgem to generate the basics for me.
Then I wrote my first stories:
features/command_line_client.feature
1 Feature: Command Line Client 2 In order to use Nestor 3 A developer 4 Wants an interface to the program 5 So that he can use it 6 7 Scenario: Getting Help 8 When I start nestor with "--help" 9 Then I should see "Usage: nestor [options]" 10 11 Scenario: Getting the version number 12 When I start nestor with "--version" 13 Then I should find "nestor \d+\.\d+\.\d+"
After writing those, I ran them:
1 ~/nestor (master) $ rake features 2 (in /Users/francois/nestor) 3 ...F 4 5 6 Failed: 7 8 1) 9 <"nestor: version unknown\n"> expected to be =~ 10 </nestor \d+\.\d+\.\d+/>. 11 /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/test/unit/assertions.rb:48:in `assert_block' 12 /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/test/unit/assertions.rb:500:in `_wrap_assertion' 13 /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/test/unit/assertions.rb:46:in `assert_block' 14 /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/test/unit/assertions.rb:229:in `assert_match' 15 /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/test/unit/assertions.rb:495:in `_wrap_assertion' 16 /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/test/unit/assertions.rb:221:in `assert_match' 17 ./features/step_definitions/assertions.rb:8:in `Then /^I should find "(.*)"$/' 18 features/command_line_client.feature:13:in `Then I should find "nestor \d+\.\d+\.\d+"' 19 rake aborted! 20 Command failed with status (1): [/System/Library/Frameworks/Ruby.framework/...] 21 22 (See full trace by running task with --trace)
Haha! So, Cucumber caught the fact that newgem didn’t set the new gem to return the version number immediately! Interesting.
Anyway, here are my step definitions, for the curious:
features/support/env.rb
1 require "test/unit" 2 3 World do 4 include Test::Unit::Assertions 5 end
features/step_definitions/nestor.rb
1 When /^I start nestor with "(.*)"$/ do |args| 2 %x"#{NESTOR_APP_ROOT}/bin/nestor #{args} > #{NESTOR_LOG_ROOT}/nestor.log 2>&1" 3 end
features/step_definitions/assertions.rb
1 Then /^I should see "(.*)"$/ do |data| 2 stdout = File.read(File.join(NESTOR_LOG_ROOT, "nestor.log")) 3 assert_match Regexp.new(Regexp.escape(data)), stdout 4 end 5 6 Then /^I should find "(.*)"$/ do |regexp| 7 stdout = File.read(File.join(NESTOR_LOG_ROOT, "nestor.log")) 8 assert_match Regexp.new(regexp), stdout 9 end
Notice that I really run the nestor binary to assert against the gem’s STDOUT. Redirecting the output to a log file I can open and search in really makes things easier. I had a bit of difficulty wrapping my head around this idea (of starting the binary from Cucumber), so I hope that helps other people too.