pass command-line arguments to RSpec RakeTask dynamically - ruby

The rspec command comes with several options you can use to customize
RSpec's behavior, including output formats, filtering examples, etc.
For example:
$ rspec path/to/spec_file.rb
$ rspec --example 'first example'
$ rspec --tag type:special
$ rspec -P "**/*_test.rb"
How can I do the same thing with rake spec (with full Rspec options).
My Rakefile:
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
task default: :spec
RSpec::Core::RakeTask.new(:spec)
I have been google but didn't find any complete answer for that. Thanks.

Command line arguments can be passed automatically to the ENV hash.
For example:
From command line: FOO=BAR rspec spec/*spec.rb
Inside RSpec: puts ENV["FOO"] # => "BAR"
In your Rakefile, you can use backticks to call the shell command.

You can do it but it requires some changes.
First of all you need to undefine already defined spec task if its present, then define it again.
Or use other name, like spec_with_opts. Though I went through renaming.
in Rakefile
Rake::Task["spec"].clear
RSpec::Core::RakeTask.new(:spec) do |task, args|
task.rspec_opts = ENV['RSPEC_OPTS'] if ENV['RSPEC_OPTS'].present?
task.pattern = ENV['RSPEC_PATTERN'] if ENV['RSPEC_PATTERN'].present?
task.exclude_pattern = ENV['RSPEC_EXCLUDE_PATTERN'] if ENV['RSPEC_EXCLUDE_PATTERN'].present?
end
task default: :spec
So it now can be run this way:
rake spec RSPEC_PATTERN=path/to/spec_file.rb
rake spec RSPEC_OPTS="--example 'first example'"
rake spec RSPEC_OPTS="--tag type:special"
This one wont work, you would need to use RSPEC_PATTERN
rake spec RSPEC_OPTS="-P '**/*_test.rb'"
You can find other options that can be defined in source file:
https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/rake_task.rb

Related

Passing format as parameter to rake spec

My question is similar to this , where they want to override rake spec's output format. The resolution to that question is to use the .rspec config file, which is limiting. I would like this to be a command line argument because I want this to vary on different machines.
The rspec executable has the -f option be defining format. rake spec has -f defining a rakefile. rake spec --format is invalid. Is this an oversight in rake spec? "Format" really isn't an option?
ANSWER: I'm self answering my question here. rake spec will take the SPEC_OPTS environment variable.
rake spec SPEC_OPTS="--format documentation"
A cleaner way todo this in Rakefile:
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = '--format documentation'
end

Recursively glob for RSpec test files at runtime

I have a suite of RSpec tests I want to group under the following hierarchy:
tests/
featA/
t1.rb
t2.rb
featB/
t3.rb
but when I run
$ rspec tests
I get the following:
rspec tests
No examples were matched. Perhaps {:unless=>#<Proc:0x00007f318919cc08#/usr/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:51>, :if=>#<Proc:0x00007f318919cdc0#/usr/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:50>} is excluding everything?
Finished in 0.00003 seconds
0 examples, 0 failures
I feel like I'm going mad, but there doesn't seem to be a way to get RSpec to recursively glob for test files? Does this functionality exist?
EDIT:
I have a workaround by doing this:
$ rspec `find tests -name "*.rb"`
but I suspect I shouldn't have to. Am I right?
You've exposed an oversight on my part! In rspec-1, you could say this:
spec test --pattern "**/*.rb"
But the --pattern option is missing in rspec-2. I've just added it (in development) and it will be included in the rspec-2.6.0 release.
I usually manage running RSpec on my specs via rake. The relevant portion of my Rakefile looks something like this:
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = ['--color', '-f progress', '-r ./spec/spec_helper.rb']
t.pattern = 'spec/**/*_spec.rb'
t.fail_on_error = false
end
Now rake spec runs RSpec with the appropriate options; you'll need to change t.pattern to match the specs you want to run.
Be sure to check out the RSpec2 site for more information.

Is it possible to run a single test in MiniTest?

I can run all tests in a single file with:
rake test TEST=path/to/test_file.rb
However, if I want to run just one test in that file, how would I do it?
I'm looking for similar functionality to:
rspec path/to/test_file.rb -l 25
The command should be:
% rake test TEST=test/test_foobar.rb TESTOPTS="--name=test_foobar1 -v"
Have you tried:
ruby path/to/test_file.rb --name test_method_name
No gem required:
ruby -Itest test/lib/test.rb --name /some_test/
Source: http://blog.arvidandersson.se/2012/03/28/minimalicous-testing-in-ruby-1-9
This is one of the things that bother me about the string name definition in tests.
When you have:
def test_my_test
end
you always know how your test is named so you can execute it like this:
ruby my_test -n test_my_test
But when you have something like:
it "my test" do
end
you are never sure how this test is really named internally so you can not use the -n option just directly.
To know how this test is named internally you only have an option: execute the whole file to try to figure out looking in the logs.
My workaround is (temporally) add something to the test name very unique like:
it "my test xxx" do
end
and then use the RegEx version of the '-n' parameter like:
ruby my_test.rb -n /xxx/
I'm looking for similar functionality to rspec path/to/file.rb -l 25
With Nick Quaranto's "m" gem, you can say:
m spec/my_spec.rb:25
If you are using MiniTest with Rails 5+ the best way to run all tests in a single file is:
bin/rails test path/to/test_file.rb
And for a single test (e.g. on line 25):
bin/rails test path/to/test_file.rb:25
See http://guides.rubyonrails.org/testing.html#the-rails-test-runner
You can use this to run a single file:
rake test TEST=test/path/to/file.rb
I also used
ruby -I"lib:test" test/path/to/file.rb
for better display.
There are 2 ways to do it:
Run tests 'manually' (see Andrew Grimm's answer).
Hack Rake::TestTask target to use a different tests loader.
Rake::TestTask (from rake 0.8.7) theoretically is able to pass additional options to MiniTest::Unit with a "TESTOPTS=blah-blah" command line option, for example:
% rake test TEST=test/test_foobar.rb TESTOPTS="--name test_foobar1 -v"
In practice, the option --name (a filter for test names) won't work, due to rake internals. To fix that you'll need to write a small monkey patch in your Rakefile:
# overriding the default rake tests loader
class Rake::TestTask
def rake_loader
'test/my-minitest-loader.rb'
end
end
# our usual test terget
Rake::TestTask.new {|i|
i.test_files = FileList['test/test_*.rb']
i.verbose = true
}
This patch requires you to create a file test/my-minitest-loader.rb:
ARGV.each { |f|
break if f =~ /^-/
load f
}
To print all possible options for Minitest, type
% ruby -r minitest/autorun -e '' -- --help
You can pass --name to run a test by its name or a number within its name:
-n, --name PATTERN Filter run on /regexp/ or string.
e.g.:
$ ruby spec/stories/foo_spec.rb --name 3
FAIL (0:00:00.022) test_0003_has foo
Expected: "foo"
Actual: nil
This flag is documented in Minitest’s README.
I am in Rails Version 4.2.11.3 and Ruby Version 2.4.7p357
Below one worked for me.
ruby -Itest <relative_minitest_file_path> --name /<test_name>/
If you are using Turn gem with minitest, just make sure to use Turn.config.pattern option since Turn Minitest runner doesn't respect --name option in ARGs.
I'm looking for similar functionality to:
rspec path/to/test_file.rb -l 25
There is a gem that does exactly that: minitest-line.
gem install minitest-line
ruby test/my_file -l 5
from https://github.com/judofyr/minitest-line#minitest-line
I use ruby /path/to/test -n /distinguishable word/
Edit:
-n is a shorthand for --name. distinguishable word can be any string you put in the test description, I usually use some random word that I know won't be present in other tests' descriptions.
Following will work
def test_abc
end
test "hello world"
end
This can run by
bundle exec ruby -I test path/to/test -n test_abc
bundle exec ruby -I test path/to/test -n test_hello_word
Install gem minitest-focus and use the keyword focus on test/spec like below to run only the specific test.
focus
def test
end
focus
it "test" do
end
This would not need any command line argument to be passed.

How do I globally configure RSpec to keep the '--color' and '--format specdoc' options turned on

How do I set global configuration for RSpec in Ubuntu.
Specifically so, --color and --format specdoc stay turned on, across all my projects (ie every time I run rspec anywhere).
As you can see in the docs here, the intended use is creating ~/.rspec and in it putting your options, such as --color.
To quickly create an ~/.rspec file with the --color option, just run:
echo '--color' >> ~/.rspec
One can also use a spec_helper.rb file in all projects. The file should include the following:
RSpec.configure do |config|
# Use color in STDOUT
config.color = true
# Use color not only in STDOUT but also in pagers and files
config.tty = true
# Use the specified formatter
config.formatter = :documentation # :progress, :html,
# :json, CustomFormatterClass
end
Any example file must require the helper to be able to use that options.
In your spec_helper.rb file, include the following option:
RSpec.configure do |config|
config.color_enabled = true
end
You then must require in each *_spec.rb file that should use that option.
If you use rake to run rspec tests then you can edit spec/spec.opts
http://rspec.info/rails/runners.html
Or simply add alias spec=spec --color --format specdoc to your ~/.bashrc file like me.
One thing to be aware of is the impact of the different ways of running RSpec.
I was trying to turn on the option with the following code in spec/spec_helper.rb -
Rspec.configure do |config|
config.tty = $stdout.tty?
end
calling the 'rspec' binary directly - or as 'bundle exec rspec' and checking $stdout.tty? will return true.
invoking the 'rake spec' task - or as 'bundle exec rake spec' - Rake will invoke rspec in a separate process, and $stdout.tty? will return false.
In the end I used the ~/.rspec option, with just --tty as its contents. Works well for me and keeps our CI server output clean.

How do you run a specific test with test/spec (not a specific file, but a spec within a given file)?

With Test::Unit, I can run:
ruby path/to/test.rb --name=test_name_that_i_want_to_run
Thus far, I have not been able to figure out how to do this with test/spec specifications. I am wondering if the way that specifications are automatically named does not allow me to do something like this.
Take the following spec for example:
require 'rubygems'
require 'spec'
describe 'tests' do
it 'should be true' do
1.should == 1
end
it 'should be false' do
1.should_not == 2
end
end
You can execute a single spec by using the -e flag and providing the portion specified by the it block. e.g. ruby my_spec.rb -e 'should be false'
After contacting the gem maintainer, Christian Neukirchen, I found out how to do this, so I am documenting it here for future reference.
specrb path/to/test.rb --name ".*should behave this way.*"
I needed to use the specrb test runner, an extended version Test::Unit's test runner, rather than just the ruby command.
You can also do this with the ruby command:
ruby path/to/test.rb -n "/should behave this way/"

Resources