News & Insights

Printing output in cucumber’s after scenario hook

While enjoying the facets of Ruby and BDD with cucumber, you might come across a puzzling issue.  Cucumber offers hooks for various events during testing, such as hooking before a scenario is run, after a scenario, and providing room for designs of your own fancy before & after your test’s execution by capturing a tag with the ‘Around’ syntax, for example.  All of these hooks are helpful, but there’s a quirk with hooking after a scenario which may leave you wondering if your hook was even executed at all.

The syntax for an after hook in Ruby looks like this:

After do |scenario|

puts “scenario ended”

end

After adding similar code to your ruby steps, you may notice that of your 10 finished scenarios, you only count 9 “scenario ended” prints.  If you only run one scenario, you’ll notice your string isn’t printed at all.  Cucumber does some interesting work behind the scenes with regard to ‘puts’.

When you run any scenario, cucumber is very kind in placing your step definition output immediately following the feature’s step.  Inlining these prints comes at a cost: to do this, cucumber replaces the default implementation of ‘puts’ with a buffered approach.  Calls to ‘puts’ during your cucumber tests will actually write your strings to a cucumber buffer, which is then flushed at the appropriate time:  after a scenario step, at the end of a scenario, etc.  The exception is after the last scenario step, the ‘puts’ buffer is not flushed, and your strings will never see the light of day.  A simple workaround?

STDOUT.puts “hello world”

Directly calling ‘puts’ on the standard stream will not use cucumber’s buffered implementation, and accordingly, STDERR.puts works as well.  Next time you wonder where your strings went, try to remember where you “puts” them.

Related Reads

Connamara Unveils QuickFIX/n 1.12.0, Enhancing Developer Experience

July 24, 2024 Connamara, a leading provider of advanced technology solutions for the financial industry, is thrilled to unveil the.

How Our Acceptance Tests Safeguard Complex Systems

By Kevin Siamis Capital markets operate within intricate systems comprising trading platforms, data feeds, and regulatory frameworks. Yet, modifying these.

Unleashing the Power of Multilingual Programming: Empowering Polyglottal Developers

Kevin Siamis breaks down our tech stack strategy.

.NET: Decoded

What’s the best use of .NET? Is the language outdated or just right for your legacy system? At Connamara, we’re.

Java: Decoded

What’s the best use of Java? Is the language outdated or just right for your legacy system? At Connamara, we’re.

C++: Decoded

What’s the best use of C++? Is the language outdated or just right for your legacy system? At Connamara, we’re.

C: Decoded

What’s the best use of C? Is the language outdated or just right for my legacy system? At Connamara, we’re.

Python: Decoded

Is Python the right technology to back your fintech or capital markets project? At Connamara, our engineering team often gets.

Connamara Announces QuickFIX/Go v0.9.0 Now With Support for Golang v1.21, Weekday Configurations, and MessageStore Enhancements

November 15, 2023 Connamara, a leading provider of advanced technology solutions for the financial industry, is excited to unveil the.

Rust: Decoded

Connamara prides itself on having a stable of polyglottal programmers ready to tackle your next custom software project. But, our.