gpt4 book ai didi

ruby - 使用 Ruby 测试单元在一个脚本中并行运行多个测试

转载 作者:数据小太阳 更新时间:2023-10-29 07:08:16 27 4
gpt4 key购买 nike

我在一个 ruby​​ 脚本中有 4 个测试,我使用命令运行它们

ruby test.rb

输出看起来像

Loaded suite test
Started
....

Finished in 50.326546 seconds.

4 tests, 5 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed

我想要实现的是,并行运行所有 4 个测试,而不是按顺序运行。大约 4 个线程,每个线程运行一个测试,有效地将执行时间减少到 4 个测试中最慢的一个 + 并行执行的时间很短。

我遇到了 this ,但这似乎并行运行多个 ruby​​ 测试文件 - 假设我有 test1.rb、test2.rb test3.rb,那么所有三个文件都将并行运行。

我们将不胜感激。

最佳答案

我尝试了 TestSuiteThread 的组合:

gem 'test-unit'
require 'test/unit'
require 'test/unit/ui/console/testrunner'
# we're running the tests, so we don't want Test::Unit to automatically run everything for us. See http://www.natontesting.com/2009/07/21/stop-rubys-testunit-suite-files-running-all-your-tests/
Test::Unit.run = true


class MyTest < Test::Unit::TestCase
def test_1()
assert_equal( 2, 1+1)
end
def test_2()
assert_equal( 2, 4/2)
end
def test_3()
assert_equal( 1, 3/2)
end
def test_4()
assert_equal( 1.5, 3/2.0)
end
end

#create TestSuites.
test_1 = Test::Unit::TestSuite.new("Test 1")
test_1 << MyTest.new('test_1')
#Same a bit shorter
test_2 = Test::Unit::TestSuite.new("Test 2") << MyTest.new('test_2')
test_3 = Test::Unit::TestSuite.new("Test 3") << MyTest.new('test_3')
test_4 = Test::Unit::TestSuite.new("Test 4") << MyTest.new('test_4')


#run the suites
Thread.new{Test::Unit::UI::Console::TestRunner.run(test_1)}
Thread.new{Test::Unit::UI::Console::TestRunner.run(test_2)}
Thread.new{Test::Unit::UI::Console::TestRunner.run(test_3)}
Thread.new{Test::Unit::UI::Console::TestRunner.run(test_4)}

看起来不错,但我没有做基准测试。

输出(见下文)有点困惑,每个线程都将自己的消息发布到其他线程的消息中,但它似乎工作正常。所以也许您必须捕获每个线程的输出以获得更好的测试日志。

Loaded suite Test 4Loaded suite Test 1Loaded suite Test 2Loaded suite Test 3
Started
Started
.
Started
.
Started

.
.
Finished in 0.328125 seconds.

Finished in 0.328125 seconds.




1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
Finished in 0.765625 seconds.
Finished in 0.546875 seconds.
100% passed
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications



3.05 tests/s, 3.05 assertions/s
100% passed
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications

3.05 tests/s, 3.05 assertions/s

100% passed
100% passed

关于ruby - 使用 Ruby 测试单元在一个脚本中并行运行多个测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10556318/

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