Jorge Israel Peña
Jorge Israel Peña

Reputation: 38576

Disable a group of tests in rspec?

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?

Upvotes: 119

Views: 58734

Answers (7)

Pyro
Pyro

Reputation: 2191

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 }

Upvotes: 202

PhilT
PhilT

Reputation: 4364

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.

Upvotes: 0

Robert Speicher
Robert Speicher

Reputation: 15552

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"

Upvotes: 48

botimer
botimer

Reputation: 641

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.

Upvotes: 22

Matt
Matt

Reputation: 21

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

Upvotes: 1

Amir Samakar
Amir Samakar

Reputation: 505

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

Upvotes: 4

GutenYe
GutenYe

Reputation: 3389

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 

Upvotes: 9

Related Questions