Skip to content

Instantly share code, notes, and snippets.

@SamMolokanov
Last active February 21, 2019 13:11
Show Gist options
  • Save SamMolokanov/713efc170d4ac36c5d5a16024ce633ea to your computer and use it in GitHub Desktop.
Save SamMolokanov/713efc170d4ac36c5d5a16024ce633ea to your computer and use it in GitHub Desktop.
Running Rspec tests multiple times with different configuration

Doing refactoring it is nice to run a bunch of tests for the legacy code and a new one. We can duplicate Rspec test cases by copy-pase or we can re-run the same tests providing a variable with different values, for example base_path for requests.

# spec/examples/multiple/foo_spec.rb
# This is an actual test
require "spec_helper"
describe "Foo" do
it "has one of available values" do
expect(variable).to be_in([:foo, :bar, :baz])
end
end
# spec/support/multiple_run_helper.rb
# This module duplicates examples which have :multiple_run in the metadata
module MultipleRunHelper
VARIABLE_VALUES = [:foo, :bar, :baz]
RSpec.shared_context "Multiple Run variable" do
let(:variable) { |example| example.metadata[:variable] }
end
RSpec.configure do |config|
config.on_example_group_definition do |group|
if group.metadata[:multiple_run]
group.descendants.each do |desc_group|
desc_group.examples.clone.each do |example|
# clone example for every variable
VARIABLE_VALUES.each { |variable| example.duplicate_with(variable: variable) }
# remove example without variable
desc_group.remove_example(example)
end
end
# add a common variable
group.include_context("Multiple Run variable")
end
end
end
end
$rspec spec/examples/multiple/foo_spec.rb -f d
Randomized with seed 62690
Foo
has one of available values
has one of available values
has one of available values
Finished in 0.03669 seconds
3 examples, 0 failures
# spec/spec_helper.rb
# This adds :multiple_run to metadata for duplicated tests
RSpec.configure do |config|
config.define_derived_metadata(file_path: %r{/spec/examples/multiple}) do |metadata|
metadata[:multiple_run] = true
end
config.include(MultipleRunHelper, multiple_run: true)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment