codedecoder

breaking into the unknown…

skip test with rspec default run

Leave a comment

We have adopted build process for our application. It run the rspec test, every time we push any code to git master branch and send emails to listed developers if the rspec fails. The error email become frequent due to failures of 3 spec written to test openam sso functionality. Those three tests are failing due to openam API problem, say the openam server is down or so on. Since, we do not have control over that, it is decided to skip these test with rspec default run.

Rspec provides tag feature to tag specs. You can do it at each spec level or context level or at the describe block. you can find more detail at rspec core documentation here

I have described my tag as below at context level

describe Users::OmniauthCallbacksController do
   describe “GET ‘saml'” do
       context “openam authentication”, :api_openam => true do

          it “user should be able to signin with openam” do
           ….write the test…
          end

          it “user should be able to logout with openam” do
          ….write the test…
         end

         it “openam logout should destroy local session also” do
          ….write the test…
        end
     end
    end
end

You can attach a tag, similarly at each spec level as below

it “openam logout should destroy local session also”,  :api_openam => true do
….write the test…
end

Also. the tag is basically have name => value syntex i,e it is not necessary to put true, if some thing else suits better say you want value to be api, then you can write :api_openam => “api” instead of :api_openam => true

The difference, is the way you can use this tag to run the rspec

To run the test which contain api_openam tag, you will just pass –tag api_openam option as below

$ rspec spec/controllers/users/omniauth_callbacks_controller_spec.rb –tag openam
Run options: include {:api_openam=>true} #thus the test is run which  contain the api_openam tag

To run the test which do not contain api_openam tag, you will just pass –tag ~api_openam option as below

$ rspec spec/controllers/users/omniauth_callbacks_controller_spec.rb –tag ~openam
Run options: excludee {:api_openam=>true} #thus the test is run which do not contain the api_openam tag

So, this is the basic of how we can run a rspec from console with tag or without tag which will filter your spec accordingly. but I need to skip some spec or test by default whenever rspec is run. This can be handle with rspec configuration in spec_helper.rb file. detail of all the available rspec configuration is available here

What we need for our case is do the configuration for filter_run_excluding. I have added it to spec_helper.rb with below line

config.filter_run_excluding :api_openam => true

Now when you run rspec with default i,e without any options, it will exclude the test having openam tag

$ rspec
Run options: exclude {:api_openam=>true} #thus the test is run which do not contain the api_openam tag

Advertisements

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s