我有一个不稳定的 RSpec 测试。为了重现失败,我想尽可能快地运行测试数百次。如何并行多次运行一个示例?
有没有更好的方法来重现不稳定的测试失败?
我通过将不稳定的代码包装在
中来调试不稳定的测试begin
... some code that I think is flaky
rescue => e
require 'debug'; binding.break
end
然后多次运行规范。 例如
# bash
for run in {1..10}; do
bundle exec rspec spec/some/file/path.rb
done
或者,您可以通过包装适当的上下文或它块在 RSpec 中调用测试 100 次。 例如
100.times do
it '...' do
# some test code
end
end
如果你有 Ruby 3.0,你可以试试 Ractor 吗?
VIRTUAL_CPU_COUNT = Etc.nprocessors
RUN_COUNT = 100
ractors = []
VIRTUAL_CPU_COUNT.times do |i|
ractors << Ractor.new(i) do |i|
puts "In Ractor #{i}"
(RUN_COUNT / VIRTUAL_CPU_COUNT).times do |t|
`bundle exec rspec spec/some/file/path.rb`
end
puts "Finished Ractor #{i}"
"i: #{i}" # implicit return value, or use Ractor.yield
end
end
values = ractors.map(&:take)
虽然我不知道如何进行调试。
还有 gem https://rubygems.org/gems/parallel_tests,这可能有帮助?