Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running a Unit test from irb or pry

Is there a way to run a testfile from inside an irb or pry session?

I tried load './testfile.rb, but that doesn't run the tests in the testfile.

My Testfile looks like this:

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
like image 853
FlyingFoX Avatar asked May 19 '12 22:05

FlyingFoX


1 Answers

Tests are not really meant to be run interactively like that, but if you look in the ruby standard library and look at the file test/unit.rb you will see that it sets an at_exit handler when you require 'test/unit':

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

So looking at this all you need to do in your irb session is call:

Test::Unit::AutoRunner.run

This will run all tests you have loaded that are subclassed from Test::Unit::TestCase.

If you just want to run the test file, without loading it into the irb session, you could simply do:

system 'ruby testfile.rb'
like image 168
Casper Avatar answered Sep 25 '22 05:09

Casper