Read more

Ruby: A short summary of available hooks in Cucumber

Emanuel
October 21, 2020Software engineer at makandra GmbH

Here is a short summary of Cucumber hooks in Ruby taken from https://github.com/cucumber/cucumber-ruby Show archive.org snapshot . Note that the BeforeStep is currently not existing in the Ruby implementation of Cucumber.

Illustration money motivation

Opscomplete powered by makandra brand

Save money by migrating from AWS to our fully managed hosting in Germany.

  • Trusted by over 100 customers
  • Ready to use with Ruby, Node.js, PHP
  • Proactive management by operations experts
Read more Show archive.org snapshot

Before hooks run before the first step of each scenario.

Before do |scenario|
  ...
end

After hooks run after the last step of each scenario, even when the step result is failed, undefined, pending or skipped.

After do |scenario|
  ...
end
Around do |scenario, block|
  ...
  block.call
  ...
end
AfterStep do |scenario|
  ...
end

You can restrict the hooks to one or more tags:

Before("@slow") do |scenario|
  ...
end

A global hook will run once before any scenario is run.

# env.rb
...

BeforeAll hooks will run before any scenario is executed.

BeforeAll do
  ...
end

You can include code to Cucumber in the World module without polluting the application namespace (example from https://jam.cucumber.io/):

features/support/helpers.rb

module Helpers
  def helper_method
    42
  end
end

World(Helpers)

features/step_definitions/step.rb

Then /^the helper method is called$/ do
  expect(helper_method).to eql(42)
end
Emanuel
October 21, 2020Software engineer at makandra GmbH
Posted by Emanuel to makandra dev (2020-10-21 11:45)