[rspec-users] how to pass command-line parameter to Rspec script?

745 views
Skip to first unread message

Zhong

unread,
Apr 18, 2011, 11:58:07 PM4/18/11
to rspec...@rubyforge.org
Dear all,

Anyone know how to pass command-line parameter to Rspec script?

For example:
I want to run the test.rb script. Suppose there is a parameter that is
set in test.rb script is sleep. For the convenience,I want to set the
parameter for sleep in the command-line.
If type : $ spec test.rb --sleep 10
The script will sleep 10 seconds.
If type $ spec test.rb --sleep 5
The script will sleep 5 seconds.

Anyone can describe the detail how to do that?

Thanks in advance!
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

David Chelimsky

unread,
Apr 20, 2011, 1:59:57 AM4/20/11
to rspec-users
On Apr 18, 2011, at 10:58 PM, Zhong wrote:

> Dear all,
>
> Anyone know how to pass command-line parameter to Rspec script?
>
> For example:
> I want to run the test.rb script. Suppose there is a parameter that is
> set in test.rb script is sleep. For the convenience,I want to set the
> parameter for sleep in the command-line.
> If type : $ spec test.rb --sleep 10
> The script will sleep 10 seconds.
> If type $ spec test.rb --sleep 5
> The script will sleep 5 seconds.
>
> Anyone can describe the detail how to do that?

You can't pass arbitrary arguments to the rspec command, but you can set an environment variable like this:

SLEEP=10 rspec test.rb

Then within the script, the value of ENV["SLEEP"] is "10", so you can say:

sleep(ENV["SLEEP"].to_f)

HTH,
David

Reply all
Reply to author
Forward
0 new messages