How to tell rspec to run without pending tests output?
Asked Answered
Y

4

16

Is there a way (maybe some key) to tell rspec to skip pending tests and don't print information about them?

I have some auto generated tests like

pending "add some examples to (or delete) #{__FILE__}"

I run "bundle exec rspec spec/models --format documentation" and get somethin like this:

Rating
  allows to rate first time
  disallow to rate book twice

Customer
  add some examples to (or delete) /home/richelieu/Code/first_model/spec/models/customer_spec.rb (PENDING: No reason given)

Category
  add some examples to (or delete) /home/richelieu/Code/first_model/spec/models/category_spec.rb (PENDING: No reason given)
......

I want to keep this files, cause I gonna change them later, but for now I want output like:

Rating
  allows to rate first time
  disallow to rate book twice

Finished in 0.14011 seconds
10 examples, 0 failures, 8 pending
Yokel answered 3/1, 2014 at 18:52 Comment(0)
A
11

Take a look at tags -

You could do something like this in your test file

describe "the test I'm skipping for now" do     
  it "slow example", :skip => true do
    #test here
  end
end

and run your tests like this:

bundle exec rspec spec/models --format documentation --tag ~skip

where the ~ character excludes all tests with the following tag, in this case skip

Aldehyde answered 3/1, 2014 at 20:38 Comment(5)
Thx. So u can't just skip all pending? Only way to add flags to each?Yokel
Actually, I'm not sure - try just running bundle exec rspec spec/models --format documentation --tag ~skip without changing your tests and see if it works, if not, just add tags to the tests that should be skippedAldehyde
@GriwMF, rather than adding the skip tag on the command line, you can create a global rspec config file at ~/.rspec with '--tag "~skip" in it. As a bonus, this will skip them in guard as well.Embroil
Just adding my two cents here: in spec_helper (if using a spec_helper file) config.filter_run_excluding skip: true I usually add it directly underneath the opening RSpec.configure do |config| line. Doesn't that achieve the same result?Barometer
Add --tag ~skip to project .rspec file to set as default for repo.Bonaventure
C
7

For posterity: you can suppress output of pending tests in the main body of documentation output by creating a custom formatter.

(For RSpec 3). I made a house_formatter.rb file in my spec directory like this:

class HouseFormatter < RSpec::Core::Formatters::DocumentationFormatter
   RSpec::Core::Formatters.register self, :example_pending
   def example_pending(notification); end
end

Then I added the following line to my .rspec file:

--require spec/house_formatter

Now I can call the formatter with rspec --format HouseFormatter <file>.

Note that I still get the "pending tests" section at the end. But as far as I am concerned, that's perfect.

Chasten answered 5/9, 2015 at 12:31 Comment(3)
you can add def dump_pending(notification); end to squelch the pending paragraphs. and use ProgressFormatter instead of DocumentationFormatter.Incestuous
Thank you! I have created an issue on rspec github repo as I think there should be a command line option to do just this. github.com/rspec/rspec-core/issues/2377Excruciation
@MarkoAvlijaš ...aaaand, they closed it. :( They did post a better fix than mine, though, so I've cheekily made that code a new answer to this question. Maybe that will help someone...Chasten
C
6

This is the official "fix" posted for this problem on Github in response to the issue Marko raised, and as such it deserves a seperate answer.

This is probably the better answer, too; mine is pretty fragile. Credit for this should go to Myron Marston on the Rspec team.

You can implement this for yourself pretty easily:

module FormatterOverrides
  def example_pending(_)
  end

  def dump_pending(_)
  end
end

RSpec::Core::Formatters::DocumentationFormatter.prepend FormatterOverrides

Or if you only want to silence block-less examples:

module FormatterOverrides
  def example_pending(notification)
    super if notification.example.metadata[:block]
  end

  def dump_pending(_)
  end
end

RSpec::Core::Formatters::DocumentationFormatter.prepend FormatterOverrides

Or, if you just want to filter out block-less pending examples (but still show other pending examples):

RSpec.configure do |c|
  c.filter_run_excluding :block => nil
end
Chasten answered 26/1, 2017 at 8:51 Comment(4)
where do you add these modules?Calamus
@AustinRichardson You can put it in spec_helper.rb -- take a look at the github thread behind the word "issue" above.Chasten
Put this in my spec helper with no affect. Any advice? I am using parallel_tests. Maybe that has something to do with it?Iberia
In fact, just ran without parallel_tests and still output pending.Iberia
B
0

The best solution I have found for entire files is to change the name of the file:

foo_spec_skipped.rb
Bonaventure answered 7/4, 2024 at 14:57 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.