gpt4 book ai didi

ruby - Minitest i_suck_and_my_tests_are_order_dependent

转载 作者:行者123 更新时间:2023-12-02 04:20:52 30 4
gpt4 key购买 nike

我正在使用 minitest 的规范语法和描述 block 。在我的 minitest_helper.rb (每个规范文件需要)中,我有:

Minitest::Test.i_suck_and_my_tests_are_order_dependent!

这使得描述 block 内的测试按顺序运行。尽管如此,描述 block 的顺序是随机的。

如何使一切井然有序?


演示问题:

require 'minitest/autorun'
require 'minitest/documentation'
Minitest::Test.i_suck_and_my_tests_are_order_dependent!

describe "Block1" do

3.times.each do |i|
it "should print #{i+1}"
end
end
describe "Block2" do

3.times.each do |i|
it "should print #{i+1}"
end
end

我希望它输出:

Block1
0001 should print 1
0002 should print 2
0003 should print 3
Block2
0001 should print 1
0002 should print 2
0003 should print 3

但有时Block2先出现。添加更多 block 表明这些 block 通常按随机顺序运行。


我要这个做什么?

Documentation

随机化测试非常适合确保测试应该是独立的(换句话说,它非常适合调试测试套件)。不过,在调试完测试套件后,我希望测试套件能够作为文档,并且以随机顺序编写的文档很糟糕

最佳答案

对于那些仍在寻找方法的人,请将其放入您的 test_helper.rb

# disable random test order for any TestCase
ActiveSupport::TestCase.class_eval do
# http://docs.seattlerb.org/minitest/Minitest/Test.html#method-c-i_suck_and_my_tests_are_order_dependent-21
i_suck_and_my_tests_are_order_dependent!
end

MiniTest.remove_possible_singleton_method(:__run)
Minitest.define_singleton_method(:__run) do |reporter, options|
suites = Minitest::Runnable.runnables.reject { |s| s.runnable_methods.empty? }
parallel, serial = suites.partition { |s| s.test_order == :parallel }

# If we run the parallel tests before the serial tests, the parallel tests
# could run in parallel with the serial tests. This would be bad because
# the serial tests won't lock around Reporter#record. Run the serial tests
# first, so that after they complete, the parallel tests will lock when
# recording results.
serial.map { |suite| suite.run reporter, options } +
parallel.map { |suite| suite.run reporter, options }
end

来自:https://www.gregnavis.com/articles/how-to-reduce-test-interference-in-minitest.html

关于ruby - Minitest i_suck_and_my_tests_are_order_dependent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30558791/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com