# What runs when running tests?

**URL:** <https://www.ruby-forum.com/t/what-runs-when-running-tests/85571>\
**Category:** Ruby\
**Created:** [February 14, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/what-runs-when-running-tests/85571 "2007-02-14T17:40:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ac91a4/32.png) [@kelly](https://www.ruby-forum.com/u/kelly)\
**Post date:** [February 14, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/what-runs-when-running-tests/85571/1 "2007-02-14T17:40:57Z")

</div>

I guess I’m still learning ruby.

While looking through the test code I realized I don’t see what  
actually _runs_.

My tests extend Test::Unit::TestCase, and define tests as

def test\_sometestname

# some testing stuff

end

I scanned Test::Unit::TestCase and I see a bunch of function  
definitions, but I don’t see what runs. I guess I’m looking for a  
method or piece of code that starts running, initializes things, finds  
the test\_\* test methods and starts running them.

It’s probably staring me in the face. Please point it out to me.

Thank you.

-Kelly

---

<div class="post-metadata">

**Author:** ![kelly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ac91a4/32.png) [@kelly](https://www.ruby-forum.com/u/kelly)\
**Post date:** [February 14, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/what-runs-when-running-tests/85571/2 "2007-02-14T17:47:57Z")

</div>

On 2/14/07, kelly [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I guess I’m still learning ruby.
> 
> While looking through the test code I realized I don’t see what  
> actually _runs_.

A TestRunner.

See the files in lib/1.8/test/unit for more information if you want to  
understand Test::Unit.

-austin

---

<div class="post-metadata">

**Author:** ![kelly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ac91a4/32.png) [@kelly](https://www.ruby-forum.com/u/kelly)\
**Post date:** [February 14, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/what-runs-when-running-tests/85571/3 "2007-02-14T18:23:06Z")

</div>

On 2/14/07, kelly [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I scanned Test::Unit::TestCase and I see a bunch of function  
> definitions, but I don’t see what runs. I guess I’m looking for a  
> method or piece of code that starts running, initializes things, finds  
> the test\_\* test methods and starts running them.

To expand on Austin’s answer just a little: I think the magic that  
you’re looking for is at the tail end of unit.rb, in the  
lib/1.8/test/unit directory of the standard library. Test::Unit takes  
advantage of the at\_exit() method to register a little bit of code  
that invokes the Test::Unit AutoRunner class right before the  
interpreter exits.

---

<div class="post-metadata">

**Author:** ![kelly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ac91a4/32.png) [@kelly](https://www.ruby-forum.com/u/kelly)\
**Post date:** [February 14, 2007, 6:32pm UTC](https://www.ruby-forum.com/t/what-runs-when-running-tests/85571/4 "2007-02-14T18:32:07Z")

</div>

Thanks.

Here is what I’ve learned so far:

I’m testing rails code. The generated rails test stubs have this line:

require File.dirname( **FILE** ) + ‘/…/test\_helper’

app/test/test\_helper.rb contains this line:

require ‘test\_help’

lib/ruby/gems/1.8/rails-1.1.6/lib/test\_help.rb contains this line:

require ‘test/unit’

lib/ruby/1.8/test/unit.rb contains these lines:

at\_exit do  
unless $! || Test::Unit.run?  
exit Test::Unit::AutoRunner.run  
end  
end

I think this is the magic that runs the tests.

-Kelly

---

<div class="post-metadata">

**Author:** ![kelly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ac91a4/32.png) [@kelly](https://www.ruby-forum.com/u/kelly)\
**Post date:** [February 14, 2007, 6:42pm UTC](https://www.ruby-forum.com/t/what-runs-when-running-tests/85571/5 "2007-02-14T18:42:07Z")

</div>

Lyle,

Thanks very much.

I see the code you refer to. Now I wonder why it is done using the  
at\_exit method – seems a little obtuse to me.

My original question has been answered. Thanks again.

-Kelly

---

<div class="post-metadata">

**Author:** ![kelly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ac91a4/32.png) [@kelly](https://www.ruby-forum.com/u/kelly)\
**Post date:** [February 15, 2007, 12:55am UTC](https://www.ruby-forum.com/t/what-runs-when-running-tests/85571/6 "2007-02-15T00:55:59Z")

</div>

On Feb 14, 11:41 am, “kelly” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I see the code you refer to. Now I wonder why it is done using the  
> at\_exit method – seems a little obtuse to me.

In Python’s unit testing framework, you have to explicitly include  
this code in every one of your test files. It’s a real pain. Using the  
at\_exit hook in Ruby makes your unit tests cleaner.
