Unit Test

How Test::Unit is clever to run test even thongh there is no main
program? Thanks. 发自网易邮箱大师

On Oct 5, 2014, at 09:46, Li [email protected] wrote:

How Test::Unit is clever to run test even thongh there is no main program?

MiniTest (which provides test/unit with Ruby 1.9 and newer) uses a
couple different hooks:

  1. Test suite requires ‘minitest/autorun’
  2. 'minitest/autorun.rb’ loads the MiniTest library
  3. MiniTest library sets an “inherited” hook:
    minitest/lib/minitest.rb at master · minitest/minitest · GitHub
    https://github.com/seattlerb/minitest/blob/master/lib/minitest.rb#L254
    This hook on a parent class is called when it is subclassed by child
    class C.
  4. ‘minitest/autorun.rb’ calls MiniTest.autorun
  5. MiniTest.autorun sets an at_exit hook:
    minitest/lib/minitest.rb at master · minitest/minitest · GitHub
    https://github.com/seattlerb/minitest/blob/master/lib/minitest.rb#L44
    This hook fires when the Ruby VM is exiting.
  6. Test classes load, subclass Test::Unit or MiniTest equivalent. This
    puts them in the list of runnables.
  7. Test classes have loaded, Ruby VM starts preparing to exit.
  8. Ruby VM calls at_exit hook.
  9. at_exit hook loops through the list of test classes, and runs them.