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.
Last active
February 21, 2019 13:11
-
-
Save SamMolokanov/713efc170d4ac36c5d5a16024ce633ea to your computer and use it in GitHub Desktop.
Running Rspec tests multiple times with different configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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