使用minitest进行多次测试 [英] Multiple tests with minitest
问题描述
我有一个应用程序,其中有些规范写成了 minitest .和往常一样,我使用rake
启动它们.
I have an app with some specs written into minitest. As usual, I start them using rake
.
因为有时候我得到一个随机结果,所以我的规格可以一次通过,而另一次则失败.
Because some times I got a random results, my specs can pass one time, and fail an other time.
在这种情况下,我可以保留序列号,并在修复后稍后重播.
因为我有这种测试(结果随机),所以我通常会运行rake
很多次,只是为了确保应用程序可以正常运行.
In this case, I can keep the sequence number and replay it later, after fixing.
Because I have this kind of tests (with a random result), I generally run rake
many time, just to be sure that the app is okay.
我想知道是否有一种不错的方法来执行多个rake
测试(例如100次),并在出现任何故障或错误时将其停止?
I would like to know if there is a nice way to perform multiple rake
tests (100 times for example), and stop them if there any failure or any error?
推荐答案
我创建了一个具有随机结果的虚拟测试来模拟您的情况:
I created a dummy test with random result to simulate your situation:
#store it as file 'testcase.rb'
gem 'test-unit'
require 'test/unit'
class X < Test::Unit::TestCase
def test_1
num = rand(10)
assert_true( num < 5, "Value is #{num}")
end
end
以下任务调用测试10次,并在第一次失败后停止:
The following task calls the test 10 times and stops after the first failure:
TEST_REPETION = 10
task :test do
TEST_REPETION.times{
stdout = `ruby testcase.rb`
if stdout =~ /\d+\) Failure/
puts "Failure occured"
puts stdout
exit
else
puts 'Tests ok'
end
}
end
为了实际使用,我会改编一些部分:
For real usage I would adapt some parts:
- 代替
puts 'Tests ok'
定义一个计数器,以查看测试成功的频率 - 相反,您可以将结果存储在结果文件中吗?
- Instead
puts 'Tests ok'
define a counter to see how often the test was succussfull - Instead
puts stdout
you may store the result in a result file?
这篇关于使用minitest进行多次测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!