Logan Serman
Logan Serman

Reputation: 29870

Is it possible to send arguments to `rake cucumber` without using environment variables?

I see that it is possible to pass arguments to a rake task:

task :task_name, :arg_name do |t, args|

What I'd like to do is pass arguments into a cucumber rake task:

Cucumber::Rake::Task.new({:tags => 'db:test:prepare'}) do |t, args|
  t.cucumber_opts = ['--tags', #args?]
end

Is this sort of thing possible? This way I could do:

rake cucumber:tags['tag1', 'tag2', ...]

And have it run only those tags. Most sources say to use an environment variable, which I have done, but I'd prefer to just provide the arguments the "right" way.

Upvotes: 7

Views: 2899

Answers (2)

Kent Le
Kent Le

Reputation: 65

You can get it to work by doing something like this:

task :task_name, :arg1 do |t, args|
  Cucumber::Rake::Task.new(:run) do |t|
    t.cucumber_opts = "--format pretty --tags @#{args[:arg1]}"
  end
  Rake::Task[:run].invoke()
end

Upvotes: 5

Lenart
Lenart

Reputation: 3111

One way of doing this would be through environment variables like so: rake cucumber:tags TAGS=tag1,tag2 and then in your rake task just parse ARGV.

tags = ARGV[1].gsub(/.+=/, '')

Cucumber::Rake::Task.new({:tags => 'db:test:prepare'}) do |t, args|
  t.cucumber_opts = ['--tags', tags]
end

You can also make it more flexible like this

tags_index = ARGV.index {|a| a.start_with?('TAGS') }
tags = ARGV[tags_index].gsub(/.+=/, '')

but probably you'd be better of with something like OptionParser.

Upvotes: -1

Related Questions