按 rspec 中种子确定的顺序列出所有规格

Listing all specs in order determined by seed in rspec

我注意到一些规范间歇性地失败,具体取决于它们 运行 的顺序。

为了隔离它们,我正在寻找一个命令,我可以在其中输入种子编号,并按照种子编号确定的顺序查看带有行号的所有规格。这可能吗?使用 --format=documentation 没有提供所需的信息。

从那里我会在每次发生间歇性故障之前记下测试列表运行,并最终缩小到我的罪魁祸首。

RSpec 的 JSON 格式化程序按照 运行:

的顺序输出文件名和行号
rspec --seed 1 -f json > out.json

从生成的 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 将包含一组文件路径,例如:

["./spec/models/user_spec.rb:19", "spec/models/user_spec.rb:29"]

在我的 spec/spec_helper.rb 文件中:

我创建了一个全局变量 $files_executed 并将其初始化为一个空集:

require 'set'
$files_executed = Set.new

然后,在 RSpec.configure 块中我添加了:

config.before(:each) do |example|
  $files_executed.add example.file_path
end

还有这个:

config.after(:suite) do
  files_filename = 'files_executed.txt'
  File.write(files_filename, $files_executed.to_a.join(' '))
end

然后您可以使用文件 files_executed.txt 的内容以之前使用的顺序再次输入 rspec 命令。

您可以使用 --bisect option.

RSpec's --order random and --seed options help surface flickering examples that only fail when one or more other examples are executed first. It can be very difficult to isolate the exact combination of examples that triggers the failure. The --bisect flag helps solve that problem.