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
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’
…
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.