Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all the tags in Rspec before running the tests

I am using rspec(3.5) to run my tests and building a CLI with it. Currently considering rake. In my CLI, I would like it to be able to find all of the tags available in my spec folders and provide it as an option to run as tests BEFORE RUNNING THE RSPEC.

I've been prying through spec_helper and haven't been able to find the tag information. My understanding is, rspec CLI itself accepts all of the tags as input, it then goes through all of the files to see if each of them matches the tags, if it does, rspec will run them.

I would like to reverse this workflow -

  1. Find all the tags in my spec folder.
  2. Provide it as an option in my rake cli.
  3. Allow the user to choose the tag and run it.

Right now I'm stuck at step 1, finding all the tags. Suppose, how do I scan through all the spec folders without running the test, and expose the tags somewhere?

Sample Excerpt from my code. The ":api" is the tag I'm referring to.

describe 'Oh Title: Oh after title', :api, :data_key => "Oh my meta_data", :feature => "Oh my feature" do
    it 'oh my explanation' :data_key=> 'oh my meta_data' do |e|
    #blah blah#
    end
end
like image 456
Stanley Chan Avatar asked Sep 02 '25 05:09

Stanley Chan


1 Answers

I know this is like 2 years late, but I am in the same spot you were in. Here is what I did.

custom rspec formatter

# spec/support/formatters/ci_rspec_run_files.rb
module Formatters
  class CiRspecRunFiles
    RSpec::Core::Formatters.register self, :example_started

    def initialize(output)
      @output = output
    end

    def example_started(notification)
      @output << "#{notification.example.id}\n"
    end
  end
end

run

 $ rspec --dry-run --tag ci:flakey --require ./spec/support/formatters/ci_rspec_run_files.rb -f Formatters::CiRspecRunFiles --out tmp/flakey_specs.txt

result

$ cat tmp/flakey_specs.txt 
./spec/models/flakey_spec.rb[1:3:6:4:1]
like image 86
Michael Avatar answered Sep 04 '25 22:09

Michael