7

How to prevent Ruby's test/unit library from autorunning your tests

 3 years ago
source link: https://jonleighton.name/2012/stop-test-unit-autorun/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to prevent Ruby's test/unit library from autorunning your tests

19 February 2012

Today I had a situation where I wanted to use perftools.rb to profile a test suite, which was written with Ruby’s test/unit library.

test/unit installs an at_exit hook which runs all the tests before Ruby exits. This was problematic, because it meant that all the tests ran after perftools had already finished its profile.

So I wanted to turn off the autorunning and explicitly run the test suite. I found out how to monkey-patch test/unit to achieve this, and figured it might be useful to someone in the future, so:

Ruby 1.9

require 'test/unit'

class Test::Unit::Runner
  @@stop_auto_run = true
end

class FooTest < Test::Unit::TestCase
  def test_foo
    assert true
  end
end

Test::Unit::Runner.new.run(ARGV)

Ruby 1.8

require 'test/unit'

module Test::Unit
  def self.run?; true; end
end

class FooTest < Test::Unit::TestCase
  def test_foo
    assert true
  end
end

Test::Unit::AutoRunner.run

Comments

I'd love to hear from you here instead of on corporate social media platforms! You can also contact me privately.

Wesley Chen's avatar

Wesley Chen

7 years ago

Thanks a lot, for the ruby 1.9 version, where is the "@@stop_auto_run"? I can't find it.

Add your comment

Name:
Email (kept private):
Message (Markdown allowed):


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK