Listing all specs in order determined by seed in rspec
RSpec's JSON formatter outputs the filenames and line numbers in the order they were run:
rspec --seed 1 -f json > out.json
To get just the list of filenames with line numbers from the resulting out.json
file:
require 'json'
data = JSON.parse(File.read('out.json'))
examples = data['examples'].map do |example|
"#{example['file_path']}:#{example['line_number']}"
end
Now examples
will contain an array of file paths like:
["./spec/models/user_spec.rb:19", "spec/models/user_spec.rb:29"]
In my spec/spec_helper.rb
file:
I have created a global variable $files_executed
and initialized it to an empty set:
require 'set'
$files_executed = Set.new
Then, inside the RSpec.configure
block 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 content of the file files_executed.txt
to feed the rspec
command again with the order that was used before.