Multiple tests with minitest

只谈情不闲聊 提交于 2019-12-11 08:46:06

问题


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.

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.

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?


回答1:


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 

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:

  • Instead puts 'Tests ok' define a counter to see how often the test was succussfull
  • Instead puts stdoutyou may store the result in a result file?



回答2:


I think you should think again about your test, not about the test call. A test with a random result looks wrong for me.

What's the random factor in your test? Can you write a mock-element for the random factor and repeat the test with different values for the mock-element. So you get a "complete" test.



来源:https://stackoverflow.com/questions/7435584/multiple-tests-with-minitest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!