Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cucumber "puts" in After hook not outputting anything

Tags:

ruby

cucumber

In Cucumber, in my env.rb file, I have a before & after hook set up (well, a few of them, some linked to specific tags) but have found the after hooks don't output anything when I put a puts in them.

For example, this works:

Before do
  puts "before the scenario"
end

but this doesn't:

After do
  puts "after the scenario"
end

It seems that the after hooks do get run (as there's a particular line I'm having a problem with in the after hook & in trying to debug that, I've found this problem) but they're just not outputting anything.

All my searching has proved fruitless, can't find anyone else with similar problems. Can anyone tell if I'm doing something wrong?

like image 910
webgirl Avatar asked May 10 '12 02:05

webgirl


2 Answers

Cucumber overrides the puts message in the RbWorld class so that anything written with puts gets properly broadcasted to all formatters. In the case of the pretty formatter, these go into a delayed_messages collection, until it calls print_messages, which it appears to do after each step name has been printed, presumably so that messages appear to be nested under the step in which they were generated.

For some reason there is no final call to print_messages in the pretty formatter, I'm not sure if it's an omission or deliberate since it would look less 'pretty' to have spurious messages in the output.

Interestingly if you add a second scenario, you'll see 'after the scenario' printed as the first message when the second scenario gets run, that's the delayed_messages collection in action.

In summary, you're doing nothing wrong, it's just how Cucumber hijacks the puts method. If you're not too bothered about these messages being nicely formatted, then you can just replace puts with STDOUT.puts to bypass Cucumber's formatting.

like image 127
Jon M Avatar answered Nov 18 '22 11:11

Jon M


 Before do
     p "Go!"
     puts "Go!"
 end

 After do
     p "Stop!"
     puts "Stop!"
     $stdout.puts "Stop!"
 end

output of this snippet may help to understand why 'puts' not working in After hook.

like image 23
suvankar Avatar answered Nov 18 '22 12:11

suvankar