2

We have a rather complex integration spec setup with capybara and chrome. This leads to slow feature specs.

It would be nice if the feature specs would be executed after all other specs. Because it takes rather long time for a integration test to "boot" and find a bug which a simple request or unit test would have found way faster before.

Question: How to ensure that rspec runs the feature specs right after the other specs but sort them randomly be seed without breaking simplecov?

phortx
  • 849
  • 5
  • 21
  • question and answer at the same second? – zishe Mar 15 '18 at 15:24
  • Yes, when you open a SO question, you can activate the checkbox "Answer your own question" and provide the answer right to the question :) See https://stackoverflow.blog/2011/07/01/its-ok-to-ask-and-answer-your-own-questions/ – phortx Mar 15 '18 at 15:37

1 Answers1

4

RSpec allows to setup a custom ordering. Following entry in the spec_helper.rb will cause rspec to run all other tests before the feature specs and order them randomly by the seed without breaking simplecov:

# Setup custom ordering to ensure that feature tests are executed after all other tests.
# Within this partition the tests are seed based randomly ordered.
config.register_ordering(:global) do |items|
  features, others = items.partition { |e| e.metadata[:type] == :feature }

  random_ordering = RSpec::Core::Ordering::Random.new(config)
  random_ordering.order(others) + random_ordering.order(features)
end

Please make sure not to have --order random in the rspec call or in the .rspec file

phortx
  • 849
  • 5
  • 21
  • You can certainly do this. However, when you turn of random ordering you risk hiding certain order-related bugs. – Midwire Mar 15 '18 at 17:20
  • 1
    Random ordering isn't turned of actually. The examples are still ordered randomly, but the integration tests run after the other tests. With database cleaner and capybara there shouldn't be any order-related bugs due to the fact that capybara spawns an own server. – phortx Mar 16 '18 at 09:12