# Ruby unit test error

**URL:** <https://www.ruby-forum.com/t/ruby-unit-test-error/90796>\
**Category:** Ruby\
**Created:** [April 1, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/ruby-unit-test-error/90796 "2007-04-01T19:51:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [April 1, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/ruby-unit-test-error/90796/1 "2007-04-01T19:51:14Z")

</div>

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 method`add\_assertion’ for nil:NilClass (NoMethodError)

could anyone help?

aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [April 1, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/ruby-unit-test-error/90796/2 "2007-04-01T20:08:16Z")

</div>

Try this:

require ‘test/unit’

class UnitTests \< Test::Unit::TestCase  
def test\_true  
assert(true)  
end  
end

Ryan

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [April 1, 2007, 8:12pm UTC](https://www.ruby-forum.com/t/ruby-unit-test-error/90796/3 "2007-04-01T20:12:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [April 1, 2007, 8:58pm UTC](https://www.ruby-forum.com/t/ruby-unit-test-error/90796/4 "2007-04-01T20:58:22Z")

</div>

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:in`new’  
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:in`catch’  
from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:52:in  
`suite’  
…

Bob

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [April 1, 2007, 9:17pm UTC](https://www.ruby-forum.com/t/ruby-unit-test-error/90796/5 "2007-04-01T21:17:23Z")

</div>

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.
