Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby + Cucumber: How to execute cucumber in code?

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?

like image 833
KomodoDave Avatar asked Dec 13 '12 16:12

KomodoDave


People also ask

How does the Cucumber execution start?

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.

How do you run a Cucumber in rails?

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.

How does Cucumber execute test scripts?

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.


2 Answers

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.

like image 106
KomodoDave Avatar answered Oct 14 '22 09:10

KomodoDave


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

like image 38
pjgranahan Avatar answered Oct 14 '22 11:10

pjgranahan