How to run several tests in rspec - ruby

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.

Related

How to run specific test cases in each describe function in mocha

I have many test spec files with describe() and it(). Needs to run only some cases (it()) say that is sanity cases of each spec file. How to run all sanity cases of each describe() of all test spec files?
I am using Webdriverio and javascript.
There are two ways of doing it.
Create separate files for each type of tests and run them as per your needs.
You can utilize grep flag of mocha to tell mocha which test case to pick.
I would prefer second one as it is more extensible. Here is what you have to do:
Update summary of it blocks to include a pattern e.g. #sanity#regression etc
At the tile of running tests from command line, pass grep flag as
mocha -g "#sanity"
Mocha will check for the text passed in the command in each of the tests and will execute only the matching ones.

Override "rspec" command to run tests

This seems simple but I can't find a reputable solution via Google or searching SO.
I'm using foreman with a Rails 4 app to load ENV via a .env file. To run my tests properly, I have to execute foreman run rspec [optional files].
This gets tedious and occasionally I forget the foreman run part. I'd like to override the rspec command for a single app so that:
rspec [files] => foreman start [files]
Looked at binstubs but I don't fully understand them and they don't look exactly like what I want.
I can create a bash script that does this, but now that's specific to my local machine instead of built into the app codebase.
I ended up creating a script in my ~/scripts folder (where I store my own scripts added to my PATH).
Here's what I have in ~/scripts/tester (with executable permissions):
#!/bin/bash
#==================================================
# Created: 2014-04-01 / Updated: 2014-04-01
# Desc: Shorten syntax to run tests properly.
#==================================================
# For developers who may not use bash at all...
# $0 = filename and path
# $1 = first arg...
cmd="foreman run rspec $1"
echo "#--------------------------------------------------"
echo "# EXECUTING: $0"
echo "# '$cmd'"
echo "#--------------------------------------------------"
$cmd
I can execute with tester in my rails app directory and it will run foreman run rspec which executes all tests or I can pass in specific files or wildcard-names and it will run the matched tests.
It outputs the file location so if I pass this script on to others they know what files being run so they can modify it...I do this because I've actually run into a situation where a new developer was Googling a "tester" script for rails wondering why he couldn't find it as part of core rails...this way newbies know exactly what's being run and where the file's located at.
Chose tester as the script name because it wouldn't clash with any other known commands AFAIK.
Sample input:
tester => foreman run rspec # all specs
tester spec/models => foreman run rspec spec/models # run all model specs

Running `cucumber feature/test.feature` in ruby shows weird behaviour

#!/usr/bin/ruby
`cucumber feature/test.feature`
running the above code issue lots of cucumber feature/test.feature commands.. why ?
When i see processes list there are 30 to 50 processes running cucumber command
also ruby program never terminates
Try running your feature files from outside the 'features' folder. Think this will solve the issue.(tested using the command line)
User:project user$ ls
features
User:project user$cucumber example.feature
The first line instructs the shell to run myapp.rb, which is, AFAIU, this script itself. Namely, each execution of the script recursively runs itself again.
Try the following:
#!/usr/bin/ruby
`cucumber feature/test.feature`
Or, even better, directly from the CLI:
cucumber feature/test.feature
To run all the tests simply issue the cucumber command without args:
cucumber

Executing particular tests under ruby test framework

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'

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.

Resources