Skip to content

Instantly share code, notes, and snippets.

@davidstosik
Last active November 6, 2019 09:49
Show Gist options
  • Save davidstosik/886b350a1e1c3c2618eb9e3eb203585b to your computer and use it in GitHub Desktop.
Save davidstosik/886b350a1e1c3c2618eb9e3eb203585b to your computer and use it in GitHub Desktop.
Output the RSpec command to reproduce a test run

Output the RSpec command to reproduce a test run

How to use

  • Add rspec_reproducible_order_command.rb to lib/.
  • Use the formatter when running RSpec, in addition to usual formatters:
    rspec --format progress --format RspecReproducibleOrderCommand --out log/rspec_command.txt
  • Shortened example output:

    rspec --order random:27344 ./spec/models/user_spec.rb:4 ...

On CircleCI

In .circleci/config.yml:

      - run:
          name: RSpec
          command: |
            mkdir -p $CIRCLE_TEST_REPORTS/rspec
            bundle exec rspec \
              --format RspecJunitFormatter --out $CIRCLE_TEST_REPORTS/rspec/rspec.xml \
              --format RspecReproducibleOrderCommand --out log/rspec_command.txt \
              --format progress \
              $(circleci tests glob 'spec/**/*_spec.rb' | circleci tests split --split-by=timings | xargs)
      - run:
          name: Command for reproducible test run
          when: on_fail
          command: |
            cat log/rspec_command.txt
            #
            # Use the following command to reproduce this exact RSpec run,
            # with the same examples in the same order
            # (you can triple-click to select the whole line):
RSpec::Support.require_rspec_core "formatters/base_formatter"
class RspecReproducibleOrderCommand < RSpec::Core::Formatters::BaseFormatter
RSpec::Core::Formatters.register self, :seed, :dump_summary
def seed(notification)
self.seed_value = notification.seed if notification.seed_used?
end
def dump_summary(notification)
output.puts [
"rspec",
order_option,
notification.examples.map(&:location)
].flatten.join(" ")
end
private
attr_accessor :seed_value
def order_option
if seed_value
"--order random:#{seed_value}"
else
"--order defined"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment