Monday, March 2, 2015

Discrimination in Rspec

I'm fully against discrimination in almost all its forms. I say "almost" because, when it comes to testing with rspec in Ruby, I'm fully in favor of it. Here's why: I can tag tests in rspec and run only those tests.

This has a lot of cool use cases, but I'm using it for running rspec tests in parallel on my CI service. For example:

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

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

Then, to run only tests tagged as "slow", run rspec with this command:

  rspec --tag slow

Another option is to have rspec automatically NOT run certain examples, such as "slow":

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

Then, to run all examples:
  
  RSpec.configure do |c|
    c.filter_run_excluding slow: true unless ENV[‘ALL’]
  end

Then run rspec with this command:

  ALL=1 rspec

I can also run all tagged tests without doing any rspec configuration using this command:

  rspec . --tag type:special

Or I can run all tests EXCEPT certain tagged tests using this command:

  rspec . --tag ~type:special

Thanks to Myron Marston on StackOverflow for this nifty trick.

No comments:

Post a Comment