Disable a group of tests in rspec?

RubyRspec

Ruby Problem Overview


I have a test spec which describes a class and within that has various contexts each with various it blocks.

Is there a way I can disable a context temporarily?

I tried adding a pending "temporarily disabled" call at the very top within a context I want to disable, and I did see something about pending when I ran the spec but then it just continued to run the rest of the tests.

This is what I kind of had:

describe Something
  context "some tests" do
    it "should blah" do
      true
    end
  end

  context "some other tests" do
    pending "temporarily disabled"

    it "should do something destructive" do
      blah
    end
  end
end

but like I said it just went on to run the tests underneath the pending call.

Searching led me to this mailing list thread in which the the creator (?) of rspec says it's possible in rspec 2, which I'm running. I guess it did work but it didn't have the desired effect of disabling all of the following tests, which is what I think of when I see a pending call.

Is there an alternative or am I doing it wrong?

Ruby Solutions


Solution 1 - Ruby

To disable a tree of specs using RSpec 3 you can:

before { skip }
# or 
xdescribe
# or 
xcontext

You can add a message with skip that will show up in the output:

before { skip("Awaiting a fix in the gem") }

with RSpec 2:

before { pending }

Solution 2 - Ruby

Use exclusion filters. From that page: In your spec_helper.rb (or rails_helper.rb)

RSpec.configure do |c|
  c.filter_run_excluding :broken => true
end

In your test:

describe "group 1", :broken => true do
  it "group 1 example 1" do
  end

  it "group 1 example 2" do
  end
end

describe "group 2" do
  it "group 2 example 1" do
  end
end

When I run "rspec ./spec/sample_spec.rb --format doc"

Then the output should contain "group 2 example 1"

And the output should not contain "group 1 example 1"

And the output should not contain "group 1 example 2"

Solution 3 - Ruby

See what you think of this:

describe "something sweet", pending: "Refactor the wazjub for easier frobbing" do
  it "does something well"
  it "rejects invalid input"
end

I like to see reasons with my pending items when I'm disabling something for "a while". They serve as little comments/TODOs that are presented regularly rather than buried in a comment or an excluded example/file.

Changing it to pending or xit is quick and easy, but I prefer the hash construction. It gives you every-run documentation, is a drop-in (doesn't change describe/context/it so I have to decide what to use again later), and is just as easily removed if the decision is made or blocker is removed.

This works the same for groups and individual examples.

Solution 4 - Ruby

another one. https://gist.github.com/1300152

use xdescribe, xcontext, xit to disable it.

Update:

Since rspec 2.11, it includes xit by default. so the new code will be

# put into spec_helper.rb
module RSpec
  module Core
    module DSL
      def xdescribe(*args, &blk)
        describe *args do
          pending 
        end
      end

      alias xcontext xdescribe
    end
  end
end

Usage

# a_spec.rb
xdescribe "padding" do
  it "returns true" do
    1.should == 1
   end
end 

Solution 5 - Ruby

Use pending instead of describe. If your block is:

context "some other tests" do
  it "should do something destructive" do
    blah
  end
end

You can skip the whole block by:

pending "some other tests" do
  it "should do something destructive" do
    blah
  end
end

Solution 6 - Ruby

describe "GET /blah" do

  before(:each) { pending "Feature to be implemented..." }

  it { expect(page).to have_button("Submit") }
  it { expect(page).to have_content("Blah") }
end

Solution 7 - Ruby

Just to explain what's happening with your code. Including it where you have, it just gets evaluated (and hence run) when the file is loaded during startup. However you need it to be run when the tests run. That's why answers have suggested putting pending (RSpec 2) or skip (RSpec 3) into a before block.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJorge Israel PeñaView Question on Stackoverflow
Solution 1 - RubyPyroView Answer on Stackoverflow
Solution 2 - RubyRobert SpeicherView Answer on Stackoverflow
Solution 3 - RubybotimerView Answer on Stackoverflow
Solution 4 - RubyGutenYeView Answer on Stackoverflow
Solution 5 - RubyAmir SamakarView Answer on Stackoverflow
Solution 6 - RubyMattView Answer on Stackoverflow
Solution 7 - RubyPhilTView Answer on Stackoverflow