我有一个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
或者,您可以通过包装适当的上下文或it块,在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)
不过,我不知道如何进行调试。
还有宝石https://rubygems.org/gems/parallel_tests,这可能有帮助吗?