简体   繁体   English

在Ruby中重新运行失败的单元测试

[英]Re-run failed unit tests in ruby

I have some unit tests written using Test::Unit::TestCase, with XML generated by ci_reporter. 我有一些使用Test :: Unit :: TestCase编写的单元测试,其中ci_reporter生成了XML。 However, due to circumstances beyond my control, they may occasionally fluctuate, and randomly fail. 但是,由于无法控制的情况,它们有时可能会波动并随机失败。 I'd like to detect when a test fails, and attempt to re-run it. 我想检测测试何时失败,然后尝试重新运行它。

I tried doing this by monkey-patching 'teardown' to check 'passed?', and re-running the tests on a failure. 我尝试通过猴子修补“拆解”以检查“通过?”,然后在失败时重新运行测试来尝试执行此操作。 However, the XML output will still show the first failed case, and not the second (now passing) run. 但是,XML输出仍将显示第一个失败的情况,而不显示第二个(现在通过)的情况。

This sounds a bit like the opposite of Multiple tests with minitest 这听起来与使用minitest进行多次测试相反

Perhaps this is a possibility: Copy your test case in an own file. 也许这是可能的:将测试用例复制到自己的文件中。 As an example, try the following test: 例如,请尝试以下测试:

#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 < 2, "Value is #{num}")
  end
end 

Then define your test call in a rake task: 然后在rake任务中定义您的测试调用:

require 'rake'

TEST_REPETION = 10
task :test do
  success = false
  TEST_REPETION.times{
    stdout = `ruby testcase.rb`

    if stdout =~ /Failure/
      puts "Failure occured - redo the test"
    else
      puts 'Tests ok'
      success = true
      exit 
    end
  }

  puts "Stopped after #{TEST_REPETION} tries" unless success
end

Now the test is called, until the test succeed or TEST_REPETION are done. 现在调用测试,直到测试成功或TEST_REPETION完成。

Remarks: 备注:

  • Rake isn't needed, you may do the call without rake ( My template was a rake task) 不需要抽水,您可以不抽水就进行通话( 我的模板是抽水任务)
  • This works only, if your xml changes for each run (it must be regenerated before the test. else you test always the same). 仅当您的xml每次运行都更改时,此方法才有效(必须在测试之前重新生成它。否则,您的测试始终相同)。
  • You may store the test result ( stdout ) in a file and use it later to analyze, which tests failed and try to retest them. 您可以将测试结果( stdout )存储在文件中,并在以后使用它分析哪些测试失败,然后尝试重新测试它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM