# Asserts in aggregate objects called by a Test::Unit::TestCas

**URL:** <https://www.ruby-forum.com/t/asserts-in-aggregate-objects-called-by-a-test-testcas/60160>\
**Category:** Ruby\
**Created:** [May 10, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/asserts-in-aggregate-objects-called-by-a-test-testcas/60160 "2006-05-10T19:33:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Tanner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8dc957/32.png) [@Andrew\_Tanner](https://www.ruby-forum.com/u/Andrew_Tanner)\
**Post date:** [May 10, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/asserts-in-aggregate-objects-called-by-a-test-testcas/60160/1 "2006-05-10T19:33:24Z")

</div>

Howdy Rubyists,

I’m building a framework for automating tests so I’ve been sinking my  
teeth into Test::Unit pretty heavily. Right now I’m trying to  
automagically do a bunch of asserts in a sub object called by a  
Test::Unit::TestCase subclass similar to something like this…

require “test/unit”

class TestStuff \< Test::Unit::TestCase

def test\_stuff

```
  obj = SubAssertObj.new

  obj.verify_stuff("please_work")

```

end

end

class SubAssertObj

```
import Test::Unit::Assertions

def verify_stuff(str)

   assert_equal("please_work", str)

end

```

end

Now if the assert\_equal fails it throws the  
Test::Unit::AssertionFailedError like expected and it reports it very  
nicely in the output. However if the assert is successful in the output  
from Test::Unit I get

1 tests, 0 assertions, 0 failures, 0 errors

How can I get the assertions in the sub object to be reflected in the  
report from Test::Unit?

Thanks,

Andrew

---

<div class="post-metadata">

**Author:** ![Andrew\_Tanner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8dc957/32.png) [@Andrew\_Tanner](https://www.ruby-forum.com/u/Andrew_Tanner)\
**Post date:** [May 10, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/asserts-in-aggregate-objects-called-by-a-test-testcas/60160/2 "2006-05-10T19:46:41Z")

</div>

On May 10, 2006, at 10:31 AM, Andrew Tanner wrote:

> from Test::Unit I get
> 
> 1 tests, 0 assertions, 0 failures, 0 errors

See add\_assertion in Test::Unit::TestCase and Test::Unit::Assertions

> How can I get the assertions in the sub object to be reflected in the  
> report from Test::Unit?

Why not write it like this:

class TestStuff \< Test::Unit::TestCase

def test\_stuff  
util\_verify\_stuff ‘please work’  
end

def util\_verify\_stuff(str)  
assert\_equal ‘please work’, str  
end

end

And then use subclassing to reuse common functionality?

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![Andrew\_Tanner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8dc957/32.png) [@Andrew\_Tanner](https://www.ruby-forum.com/u/Andrew_Tanner)\
**Post date:** [May 11, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/asserts-in-aggregate-objects-called-by-a-test-testcas/60160/3 "2006-05-11T21:57:21Z")

</div>

I finally figured out how to do this this week. This problem has been  
nagging me for two years.

Sooner or later, when you build a Ruby testing framework of a certain  
degree of complexity, it seems to stop counting your assertions. So  
when you run the tests, you are incorrectly told you have no  
assertions. This is really annoying and makes you look like an idiot.  
What kind of tester doesn’t put any verification in his tests? When  
things are still working, it looks like this:

Finished in 10.285 seconds.  
6 tests, 8 assertions, 0 failures, 0 errors

But then you get 0 assertions, even though you know your assertions are  
actually being executed. Why? (And if you look closely, you’ll see they  
do get counted when they fail!)

The problem typically comes when you put assertions in your library  
modules. This isn’t hard. All you have to do is put include  
Test::Unit::Assertions in your module or helper class and then you can  
include calls to assert, assert\_equal and all the other assert methods.  
It works, except that your assertions aren’t counted.

The reason why is that the method add\_assertion does the counting. This  
is automatically called by all the assert methods. When they call the  
add\_assertion method of TestCase, the count shows up in your test  
results no problem. But the add\_assertion method of the Assertions  
module is just a no op. This is why they don’t affect the assertion  
count. The solution is to add a functional add\_assertion method back to  
your helper module/class that ties back to your test case.

Here’s how. The first thing is to keep track of the test case that is  
running. We’ll just put it in a global variable, which is ugly, but it  
works. The best place to put this is in the setup method of your test  
case.

def setup  
$testcase = self  
end

The test case is actually the object the setup method is part of, so  
self gives us a reference to it when it runs. You may already have  
other stuff in your setup method as well. That’s fine; just add this  
too.

Now we need to make a change to your helper class/module where you did  
the include Test::Unit::Assertions. Add this method:

def add\_assertion  
$testcase.add\_assertion  
end

The name for what we are doing here is “delegation,” except that we are  
doing it in a totally non-object-oriented way. We are “delegating” the  
call to add\_assertion back to the test case where it belongs. After  
you’ve made these changes, tell your developer friends that you fixed  
the assertion count problem by delegating the call to add\_assertion  
back to the test case. They’ll be impressed.

But don’t start bragging yet. If you run your code right now, you’ll  
get a complaint that you are calling a private method. I’ve seen some  
clever hacks to get around this problem, but actually Ruby makes the  
solution really easy. Just make the method public! Add this code to  
your TestCase class definition:

public :add\_assertion

Try doing that in Java! Don’t put in any of the methods; just put it  
inside the class definition. To recap, your test case should now look  
something like this:

class MyTest \< Test::Unit::TestCase  
def setup  
$testcase = self  
# other stuff maybe  
end  
public :add\_assertion  
def test\_something  
# your actual tests…

---

<div class="post-metadata">

**Author:** ![Andrew\_Tanner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8dc957/32.png) [@Andrew\_Tanner](https://www.ruby-forum.com/u/Andrew_Tanner)\
**Post date:** [May 11, 2006, 10:16pm UTC](https://www.ruby-forum.com/t/asserts-in-aggregate-objects-called-by-a-test-testcas/60160/4 "2006-05-11T22:16:10Z")

</div>

Interesting idea. Here is another possibility:

require ‘test/unit’

module TestHelper  
include Test::Unit::Assertions

def initialize(test)  
@test = test  
end

def add\_assertion  
@test.send(:add\_assertion)  
end  
end

class MyTestHelper  
include TestHelper

def test\_x  
assert(true)  
end  
end

class TC\_Delegate \< Test::Unit::TestCase  
def test\_delegating  
MyTestHelper.new(self).test\_x  
end  
end  
**END**

Using send gets around the private problem (though that isn’t  
future-proof.) I think passing in the test is better than using a  
global. Plus if you just include the TestHelper module to make use of  
this, which results in less duplicate code than your solution. There  
is probably an even slicker way to do this…

Ryan

---

<div class="post-metadata">

**Author:** ![Andrew\_Tanner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8dc957/32.png) [@Andrew\_Tanner](https://www.ruby-forum.com/u/Andrew_Tanner)\
**Post date:** [May 11, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/asserts-in-aggregate-objects-called-by-a-test-testcas/60160/5 "2006-05-11T23:22:06Z")

</div>

On May 11, 2006, at 1:13 PM, Ryan L. wrote:

> > ```
> > def test_something
> > 
> > ```
> 
> @test = test  
> def test\_x
> 
> Using send gets around the private problem (though that isn’t  
> future-proof.) I think passing in the test is better than using a  
> global. Plus if you just include the TestHelper module to make use of  
> this, which results in less duplicate code than your solution. There  
> is probably an even slicker way to do this…

I’ve never needed to have all this complexity to reuse test methods.

Making abstract Test::Unit::TestCase subclasses is much simpler than  
dispatching to some module.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
