# Why does running a Test::unit test in irb work so weirdly?

**URL:** <https://www.ruby-forum.com/t/why-does-running-a-test-unit-test-in-irb-work-so-weirdly/129326>\
**Category:** Ruby\
**Created:** [February 16, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/why-does-running-a-test-unit-test-in-irb-work-so-weirdly/129326 "2008-02-16T20:35:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e36b37/32.png) [@mith](https://www.ruby-forum.com/u/mith)\
**Post date:** [February 16, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/why-does-running-a-test-unit-test-in-irb-work-so-weirdly/129326/1 "2008-02-16T20:35:25Z")

</div>

Why can’t I run a Test::Unit::TestCase from IRB and have it run before I  
quit?  
I have found this problem mentioned by others. For an example, see this  
tutorial about testing:

[http://www.nullislove.com/2007/11/14/testing-in-rails-part-1-unit-testing-in-ruby/](http://www.nullislove.com/2007/11/14/testing-in-rails-part-1-unit-testing-in-ruby/)

> irb --simple-prompt
> 
> > require ‘test/unit’  
> > =\> true  
> > class FirstTests \< Test::Unit::TestCase  
> > def test\_addition  
> > assert(1 + 1 == 2)  
> > end
> > 
> > def test\_subtraction  
> > assert(1 - 1 == 2)  
> > end  
> > end  
> > =\> nil  
> > quit  
> > Loaded suite irb  
> > Started  
> > .F  
> > Finished in 0.00119 seconds.

The author mentions that a quirk of irb makes this happen but doesn’t  
say what the quirk is:

“When we finished the definition, nothing happened. When we exited IRB,  
then our tests ran. Thatâ€™s a characteristic of using IRB for our tests  
and wonâ€™t be significant as we move on.”

Thanks for any insight you have.

---

<div class="post-metadata">

**Author:** ![mith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e36b37/32.png) [@mith](https://www.ruby-forum.com/u/mith)\
**Post date:** [February 16, 2008, 11:47pm UTC](https://www.ruby-forum.com/t/why-does-running-a-test-unit-test-in-irb-work-so-weirdly/129326/2 "2008-02-16T23:47:20Z")

</div>

On Feb 16, 2008 11:35 AM, Peter R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why can’t I run a Test::Unit::TestCase from IRB and have it run before I  
> quit?

You can, you just have to explicitly tell it to run. Normally, you  
don’t explicitly run Test::Unit tests, you simply define them and then  
they run automagically when ruby exits, because the Test::Unit  
library, when it is loaded, loads an at\_exit routine that will (unless  
something has told it not too) run the tests that have been defined.  
Its not really an IRB quirk that stops them from running until you  
exit, its just that when you run a script with unit tests  
“standalone”, you don’t realize that the tests are run when it is  
exiting. With IRB, you just notice what is going on regularly when you  
use Test::Unit.

The RDoc for Test::Unit gives this example of how to explicitly run a  
test case:

require ‘test/unit/ui/console/testrunner’  
Test::Unit::UI::Console::TestRunner.run(TC\_MyTest)
