I'd like to execute Cucumber features from within Ruby code.
Typically the cucumber
binary installed with the gem is executed on the command line with one or more features specified.
However, I'd like to define logic that creates a dynamic feature execution flow. In other words, the program can work out which features should be executed.
Is it possible to instantiate Cucumber with specified feature files from Ruby code as opposed to the command line?
We can define each scenario with a useful tag. Later, in the runner file, we can decide which specific tag (and so as the scenario(s)) we want Cucumber to execute. Tag starts with “@”. After “@” you can have any relevant text to define your tag.
Enable Cucumber support in Rails applicationsIn the Setup Cucumber Support dialog, choose the test framework for Cucumber, specify the required options, and click OK. RubyMine will run the cucumber:install generator that sets up Cucumber in your Rails project and generates necessary files in the features directory.
Firstly, Cucumber tool reads the step written in a Gherkin or plain English text inside the feature file. Now, it searches for the exact match of each step in the step definition file. When it finds its match, then executes the test case and provides the result as pass or fail.
I discovered how from the mailing list and some API reading.
features="path/to/first.feature path/to/second.feature"
runtime = Cucumber::Runtime.new
runtime.load_programming_language('rb')
Cucumber::Cli::Main.new([features]).execute!(runtime)
If you want all features within your gem's features/
directory to be executed, pass an empty array to Main.new
instead.
To convert this example command, with features and options specified:
cucumber features/first.feature features/second.feature -d -f Cucumber::Formatter::Custom
into Ruby code, it boils down to passing Cucumber an args
array:
require 'cucumber'
# Method 1 - hardcoded features
args = %w(features/first.feature features/second.feature -d -f Cucumber::Formatter::Custom)
# Method 2 - dynamic features
features = 'features/first.feature features/second.feature'
args = features.split.concat %w(-d -f Cucumber::Formatter::Custom)
# Run cucumber
begin
Cucumber::Cli::Main.new(args).execute!
rescue SystemExit
puts "Cucumber calls @kernel.exit(), killing your script unless you rescue"
end
Tested using Ruby 2.0.0p598 and Cucumber 1.3.17
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With