Search code examples
rubyraketestunit

Set timeout to ruby Test::unit run


I am using a rake task to run tests written in Ruby. The rake task:

desc "This Run Tests on my ruby app"
Rake::TestTask.new do |t|
  t.libs << File.dirname(__FILE__)
  t.test_files = FileList['test*.rb']
  t.verbose = true
end

I would like to create a timeout so that if any test (or the entire suite) hangs a timeout exception will be thrown and the test will fail.

I tried to create a new task that would run the test task with a timeout:

desc "Run Tests with timeout"
task :run_tests do
  Timeout::timeout(200) do
    Rake::Task['test'].invoke
  end
end

The result was that a timeout was thrown, but the test continued to run.


Solution

  • This code adds a timeout for entire suite:

    def self.suite
        mysuite = super
        def mysuite.run(*args)
            Timeout::timeout(600) do
                super
            end
        end
        mysuite
    end