9

Why do i require minitest/autorun instead of test/unit for generating unit test

require 'test/unit'

class Brokened
  def uh_oh
    "I needs fixing"
  end
end

class BrokenedTest < Minitest::Test
  def test_uh_of
    actual = Brokened.new
    assert_equal("I'm all better now", actual.uh_oh)
  end
end

Running the above code, interpreter raise warning

You should require 'minitest/autorun' instead

knut
  • 27,320
  • 6
  • 84
  • 112
Rajnish
  • 419
  • 6
  • 21
  • 1
    Because `test/unit` is deprecated : http://stackoverflow.com/a/28598052/2483313 – spickermann Dec 16 '15 at 17:30
  • @spickermann i wanted to know the reason behing it, i know code would work if i replace it, but y do i have to do that. Any insight – Rajnish Dec 16 '15 at 17:32

1 Answers1

7

Your code example will end in a NameError: uninitialized constant Minitest.

You have two possibilities:

  • Use test/unit in combination with Test::Unit::TestCase or
  • use require 'minitest/autorun' in combination with Minitest::Test.

test/unit is deprecated and it is recommended to use minitest (MiniTest is faster and smaller).

If you switch the test gem you must change perhaps some more things:

  • replace require "test/unit" with require "minitest/autorun"
  • replace Test::Unit::TestCase with with Minitest::Test
  • There is no assert_nothing_raised (details)
  • assert_raise becomes assert_raises.
  • perhaps some other issues

You may use require 'minitest' instead require 'minitest/autorun' - you will get no syntax error, but there is also no test execution. If you want to execute tests, you must call them on your own (see minitest-a-test-suite-with-method-level-granularity)

Community
  • 1
  • 1
knut
  • 27,320
  • 6
  • 84
  • 112