Ruby unit test error


#1

Hi

I am trying to run some unit tests.

require 'test/unit'

class UnitTests < Test::Unit::TestCase

def initialize
assert(true)
end

end

UnitTests.new

but i am receiving this error

c:/ruby/lib/ruby/1.8/test/unit/testcase.rb:117:in add_assertion': undefined methodadd_assertion’ for nil:NilClass (NoMethodError)

could anyone help?

aidy


#2

Try this:

require ‘test/unit’

class UnitTests < Test::Unit::TestCase
def test_true
assert(true)
end
end

Ryan


#3

You will have to require the file of the class you are testing.

Also, I suspect that you aren’t testing a class named “Unit”.

If you are testing a class named “Array”, your class name would be
“ArrayTest”

Another point to make: I don’t think you have to write an
“initialize” method at any point. You can use setup() and teardown()
(?), and use test_whatever for the actual tests. I.e.

require ‘test/unit’
require ‘array’

class ArrayTest < Test::Unit::TestCase
def setup
@array = Array.new
end

def test_new_array_is_empty
assert @array.empty?
end
end

Scott


#4

You are calling an assert in the constructor of the Test. The test/
unit framework hasn’t setup the necessary stuff yet to call
assertions (namely a TestResult object) - while inside the
constructor, there are no guarantees that the object has been
completely initialized yet.

After you initialize your test, then create a test method and put
asserts in that, as Ryan suggested.

class UnitTest < Test::Unit::TestCase

def initialize(test_method_name)

super(test_method_name)

end

def test_unit_method
assert …
end
end

NOTE: if you are creating constructors on your test classes, be aware
that the test/unit framework TestCase class uses constructors with a
string param to identify individual test methods to run when it auto-
builds test suites for you. You will most likely get an error like
the following if you stray from the constructor convention:
/usr/local/lib/ruby/1.8/test/unit/testcase.rb:53:in initialize': wrong number of arguments (1 for 0) (ArgumentError) from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:53:innew’
from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:53:in
suite' from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:52:incatch’
from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:52:in
`suite’

Bob


#5

On Mon, Apr 02, 2007 at 03:11:16AM +0900, Scott T. wrote:

You will have to require the file of the class you are testing.

He’s not testing any external class. He’s just writing a skeleton test
at
this point, to check he understands how Test::Unit works (a good
strategy
IMO)

Also, I suspect that you aren’t testing a class named “Unit”.

If you are testing a class named “Array”, your class name would be
“ArrayTest”

It doesn’t make the slightest difference. He can call his test class
‘Flurble’ by all means.

Anyway, who says that a unit test suite has to test only a single class?
Many useful tests under Test::Unit are really integration tests that
exercise a whole load of classes.

Another point to make: I don’t think you have to write an
“initialize” method at any point.

That’s true.

I always found it odd about Test::Unit that you define a ‘Class’ for
your
tests, when presumably only one instance of this class is ever
instantiated.

UnitTests.new

He doesn’t need that either; the test runner finds all the classes of
interest and creates the objects by itself.

Regards,

Brian.