rspecrspec-railsrspec3

RSpec collection without running tests


Is there a way to trigger RSpec test case collection, but not run the tests?

In Python's pytest, the equivalent is appending a --collect-only switch.

My use case: I would like to run a hook that validates some metadata about our test cases, such as enforcing a mandatory tagging pattern, and I'd like to include this validation check as a PR check. The issue is, I don't really want the test suite to run, but I would only like to validate test case metadata.

Any help would be appreciated and thank you in advance!


Solution

  • There seems to be no built-in way to do collection only. Instead, I had to add the following in my spec_helper. Basically, skip the test if you pass in an environment variable CONTEXT=validate into your test run. Not really a great way to do this but it seems RSpec provides no built-in alternative:

      config.around(:each) do |example|
        # validate test case metadata. Ensure proper folder structure and team tagging
        # No need to run tests during validation, so they are skipped and left in 'pending' state
        if CONTEXT == 'validate'
          puts '[INFO] Skipping spec run and validating spec'
          example.skip
          validate_spec(example)
        else
          example.run
        end