Executing particular tests under ruby test framework - ruby

I have a set of test cases under ruby test framework 1.8.7
Lets say i have a ruby file named check.rb which contains differents tests like
test_a_check, test_b_check and test_c_check.
Now when i run the file ruby check.rb, all the test cases will be executed.
My part of the question is,
I want to pass a new parameter to the script while running, say ruby check.rb --sunset
based on the sunset parameter i want my script to execute only test_a_check and test_b_check and not the test_c_check.
By default, if i run the script all the tests should be excuted but when the --sunset parameter is passed only two of three tests should be executed.
is there are way i can achieve this?

If you are using minitest you can specify the method via
ruby check --name test_method_name

If it's a common testing framework, then look into it's manual, but
If it's your personal testing script, then just look in ARGV:
test_a_check
test_b_check
test_c_check if ARGV[0] != '--sunset'

Related

Is there a way to mock Facter::Core::Execution.execute output in ruby?

I have a custom fact in ruby that uses Facter::Core::Execution.execute to execute a system command and sets the fact to true or false based on the output of the execute command. Is there a way I could mock the output of the execute command to test it?
Using rspec:
expect(Facter::Core::Execution).to receive(:execute).and_return(true)

How to run several tests in rspec

I run several tests via rspec. The names are test1.rb, test2.rb, and so on. How do I run all of them in a queue and not one by one in the linux console? I tried an
rspec -e"test"
variant, but the console says all examples were filtered out. Please help.
Before I proceed, please note that all specs filename should end with _spec.rb. In your case it should be: test1_spec.rb, test2_spec.rb, and so on. I'd encourage you to checkout RSpec documentation on command line usage. To run all specs which are inside ./spec directory. You can run:
$ rspec .
You can pass --order option to run all specs in random order: $ rspec --order rand. If you want to run particular spec files then you can pass path/to/your/file:
$ rspec spec/test1_spec.rb spec/test2_spec.rb
Please check -e or --example command option, which runs the specs with description passed as argument to -e option for more clarity.

How can I make RSpec output to console when run as a command %x[rspec] from Ruby script?

I have a class with an instance method that runs RSpec using the %x[] notation:
class TestRunner
def run_rspec
# do stuff
%x[rspec spec -c -f documentation]
# do more stuff
end
end
When I do this:
> tr = TestRunner.new
> tr.run_rspec
The documentation (group and example names) does not appear in the console.
To contrast, when I run rspec straight from the command line I get this:
$ rspec spec -c -f documentation
a group name
an example
another example
...
I don't want to do this:
puts %x[rspec spec -c -f documentation
Because then the output all spits out in one huge clump at the very end. I want it to run in "real time," with each example showing up as each test is run.
Is there a way, with the setup I have, to get RSpec to announce what it's doing, as it's doing it (as it does when run normally from the command line)?
I've been advised that system() and the other shell methods can be dangerous to use, so I've opted to switch to the even-better approach of using RSpec itself:
RSpec::Core::Runner.run(['spec', '-c', '-f', 'documentation'])
rather than calling it via shell from my Ruby script.
Ruby offers several options for running programs from the command line. I was using %x[], the wrong choice for my use case.
Solution: Use system(), not %x[] -- rspec will write to STDOUT in real-time when I call it with system('rspec spec').
Some background in case it's helpful to anyone who stumbles upon this question:
Consider the differences between Ruby's command-line options:
%x[command] accumulates the result of command and returns it, in one chunk.
exec('command') will output command as command runs, but will replace whatever process called it -- i.e., if you use exec in your Ruby script, your Ruby script won't finish.
system('command') executes command in a subshell, and returns to the calling script.
This is why system was the choice for my script.

is it possible to pass through command-line options to a script in rspec

I have an RSpec script that tests a program in a different language I am developing. Since I can run and test 32 and 64-bit versions of this application, I would like to have a way to signal this on the command-line.
What I really want is to do something like this:
rspec -c myspec.rb lin32
or
rspec -c myspec.rb lin64
and have the lin32 or lin64 be passed as a string I can access in the ruby file itself. Is this possible? This site mentions environment variables but that is cumbersome. It also mentioned doing ARGV manipulation -- is this a possible way of doing it?
From David Chelimsky
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)
Try the -- parameter. It's used to tell an app to stop processing parameters and pass the remaining ones to a child process. I don't know if rspec understands it but it's worth a try.
`rspec -c myspec.rb -- lin32`
`rspec -c myspec.rb -- lin64`

Is there a way to programmatically count RSpec tests?

Given a Rakefile that executes some RSpec tests by way of a Spec::Rake::SpecTask, how can I programmatically determine the number of tests that passed, failed, and are pending once the task that ran the tests is finished?
SpecTask is just a nice wrapper around sh ruby -S spec ..., so you have no access to the formatter's meta data. You could parse the result, or do the evaluation somewhere else than your Rakefile, or not use SpecTask at all.

Resources