Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line to run all examples in RSpec, including ones that are filtered out?

Tags:

tdd

ruby

rspec

There are several examples that are slow, which are filtered out as follows:

RSpec.configure do |c|
  c.filter_run_excluding slow: true
end

describe 'get averages but takes a long time', slow: true do
  it 'gets average foo' do
    ....
  end

  it 'gets average bar' do
    ...
  end
end

This works great and does not run the slow tests.

rspec

But what is the RSpec command to run all examples from command line, including the slow one s that are filtered out?

like image 308
B Seven Avatar asked Oct 12 '12 14:10

B Seven


People also ask

How to run specific test cases in rspec?

Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.


2 Answers

If you run rspec --help, the output includes the following:

    -t, --tag TAG[:VALUE]        Run examples with the specified tag, or exclude examples
                                 by adding ~ before the tag.
                                   - e.g. ~slow
                                   - TAG is always converted to a symbol

You can run rspec --tag slow to run all the examples tagged as slow; however, that doesn't run all examples, as you want. I don't think there's a simple way to get what you want; the exclusion filter was designed for cases where you wouldn't want to override it at the command line (e.g. based on ruby version or whatever--it doesn't make sense to force run a spec that doesn't apply to your ruby version). You can open up an rspec core issue so we can discuss potential changes to add what you want. In the meantime, you can get it using environment variables:

RSpec.configure do |c|
  c.filter_run_excluding slow: true unless ENV['ALL']
end

With this setup, rspec will run all specs except the slow ones, and ALL=1 rspec will run all the specs including the slow ones.

like image 51
Myron Marston Avatar answered Oct 23 '22 05:10

Myron Marston


Excluding slow tests

If you want rake to exclude slow tests by default, Myron's answer is probably your best bet. This however is a simpler solution that will work well for most people.

# Run all tests
rspec

# Run tests, excluding the ones marked slow
rspec --tag ~slow

I use guard to run my tests while I develop. You can tell guard to exclude slow tests when it runs all tests. This way you can run only the fast tests while you develop, and you can run the full suite with rake or rake --tag slow when you want to. This is also great because your CI server can run your full suite without having to know special ENV variables to pass in.

Guardfile:

guard :rspec, cli: '--drb', run_all: {cli: '--tag ~slow'} do
  ...
end

Guard will still run a slow test when you trigger a watch for it, like when you're editing it.

like image 31
Edward Anderson Avatar answered Oct 23 '22 05:10

Edward Anderson