Listing all specs in seed order in rspec
I've noticed a couple of specs that break intermittently depending on the order in which they are executed.
To isolate them, I'm looking for a command where I can enter the seed number and see all the specs listed with line numbers, in the order determined by the seed number. Is it possible? using the documentation --format = did not provide the required information.
From there, I will notice that the list of tests is executed BEFORE the intermittent failure every time it occurs, and eventually narrows down to my culprit.
source to share
The RSpec JSON formatter outputs file names and line numbers in the order in which they were executed:
rspec --seed 1 -f json > out.json
To get just a list of filenames with line numbers from the resulting file out.json
:
require 'json'
data = JSON.parse(File.read('out.json'))
examples = data['examples'].map do |example|
"#{example['file_path']}:#{example['line_number']}"
end
examples
Will now contain an array of file paths, for example:
["./spec/models/user_spec.rb:19", "spec/models/user_spec.rb:29"]
source to share
In my spec/spec_helper.rb
file:
I created a global variable $files_executed
and initialized it to an empty set:
require 'set'
$files_executed = Set.new
Then, inside the block, RSpec.configure
I added:
config.before(:each) do |example|
$files_executed.add example.file_path
end
And this:
config.after(:suite) do
files_filename = 'files_executed.txt'
File.write(files_filename, $files_executed.to_a.join(' '))
end
And then you can use the contents of the file files_executed.txt
to re-issue the command rspec
in the order you used before.
source to share