Forum: Ruby what runs when running tests?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6559f9b9607a89074ea5e238d31ffbc2?d=identicon&s=25 kelly (Guest)
on 2007-02-14 17:40
(Received via mailing list)
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
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 Austin Ziegler (austin)
on 2007-02-14 17:47
(Received via mailing list)
On 2/14/07, kelly <railsinator@gmail.com> 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
0276239ca57aee241d4b41379587fa20?d=identicon&s=25 Lyle Johnson (Guest)
on 2007-02-14 18:23
(Received via mailing list)
On 2/14/07, kelly <railsinator@gmail.com> 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.
6559f9b9607a89074ea5e238d31ffbc2?d=identicon&s=25 kelly (Guest)
on 2007-02-14 18:32
(Received via mailing list)
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
6559f9b9607a89074ea5e238d31ffbc2?d=identicon&s=25 kelly (Guest)
on 2007-02-14 18:42
(Received via mailing list)
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
68db3bafb0a990bf605c4cf62bf85db0?d=identicon&s=25 Bret Pettichord (Guest)
on 2007-02-15 00:55
(Received via mailing list)
On Feb 14, 11:41 am, "kelly" <railsina...@gmail.com> 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.
This topic is locked and can not be replied to.