Running Unit test with irb or pry

Is there a way to run a test file inside an irb or pry session?

I tried load './testfile.rb, but this does not run the tests in the test file.

My test file is as follows:

require 'test/unit'
require './sudoku.rb'

class SudokuTest < Test::Unit::TestCase
  def test_initialize
    assert_nothing_raised do
      Sudoku.new(Array.new(9*9))
    end
    assert_nothing_raised do
      Sudoku.new(Array.new(9*9,Field.new(nil)))
    end
  end
end
+5
source share
3 answers

The tests are not really intended to be run interactively, but if you look in the standard ruby ​​library and look at the file test/unit.rb, you will see that it installs a handler at_exitwhen you require 'test/unit':

at_exit do
  unless $! || Test::Unit.run?
    Kernel.exit Test::Unit::AutoRunner.run
  end
end

Therefore, looking at this all you need to do in your session irbis called:

Test::Unit::AutoRunner.run

This will run all the tests you upload that subclass out Test::Unit::TestCase.

, irb, :

system 'ruby testfile.rb'
+4

, . .

:

tests = SudokuTest.new
tests.test_initialize
0

Perhaps plymouth is what you want. It can automatically start a Pry session in the context of a failed test.

0
source

All Articles