# Using unit test

**URL:** <https://www.ruby-forum.com/t/using-unit-test/86487>\
**Category:** Ruby\
**Created:** [March 24, 2006, 2:23am UTC](https://www.ruby-forum.com/t/using-unit-test/86487 "2006-03-24T02:23:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 24, 2006, 2:23am UTC](https://www.ruby-forum.com/t/using-unit-test/86487/1 "2006-03-24T02:23:57Z")

</div>

What is the most prevalent way of using unit testing in Ruby? Do I  
require ‘test/unit’ and add test methods to production scripts and then  
comment the testing portions out when not testing? Or, should I develop  
a ‘testing’ version of the scripts identical to the production versions?

Thanks for any advice. I’ve got the concept down… just thinking about  
implementation and actively using tests.

Brad

---

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 24, 2006, 2:38am UTC](https://www.ruby-forum.com/t/using-unit-test/86487/2 "2006-03-24T02:38:53Z")

</div>

On 3/23/06, rtilley [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What is the most prevalent way of using unit testing in Ruby? Do I  
> require ‘test/unit’ and add test methods to production scripts and then  
> comment the testing portions out when not testing? Or, should I develop  
> a ‘testing’ version of the scripts identical to the production versions?
> 
> Thanks for any advice. I’ve got the concept down… just thinking about  
> implementation and actively using tests.

I generally put my tests in a separate tests directory from my code.  
Then I have a ‘run\_tests.rb’ file that runs all the tests. Here’s the  
entirety of run\_tests.rb:

```
    Dir["tests/*"].each { |f| require f }

```

Or, you could somehow use Rake to run the tests, like Rails does.

Joe

---

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 24, 2006, 2:49am UTC](https://www.ruby-forum.com/t/using-unit-test/86487/3 "2006-03-24T02:49:54Z")

</div>

On Mar 23, 2006, at 5:38 PM, Joe Van D. wrote:

> > about  
> > implementation and actively using tests.
> 
> I generally put my tests in a separate tests directory from my code.  
> Then I have a ‘run\_tests.rb’ file that runs all the tests. Here’s the  
> entirety of run\_tests.rb:
> 
> ```
> Dir["tests/*"].each { |f| require f }
> 
> ```
> 
> Or, you could somehow use Rake to run the tests, like Rails does.

Even easier:

$ ls  
test/  
$ ls test  
test\_something.rb  
$ testrb test  
Loaded suite test  
Started  
.  
Finished in 0.012917 seconds.

1 tests, 1 assertions, 0 failures, 0 errors

–  
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:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 24, 2006, 3:04am UTC](https://www.ruby-forum.com/t/using-unit-test/86487/4 "2006-03-24T03:04:03Z")

</div>

rtilley wrote:

> What is the most prevalent way of using unit testing in Ruby? Do I  
> require ‘test/unit’ and add test methods to production scripts and then  
> comment the testing portions out when not testing? Or, should I develop  
> a ‘testing’ version of the scripts identical to the production versions?
> 
> Thanks for any advice. I’ve got the concept down… just thinking about  
> implementation and actively using tests.
> 
> Brad

Put the tests in a separate directory alongside the code. Chapter 12 of  
the Pickaxe includes some “best practices” for unit testing Ruby with  
Test::Unit.

---

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 29, 2006, 1:29am UTC](https://www.ruby-forum.com/t/using-unit-test/86487/5 "2006-03-29T01:29:02Z")

</div>

On 2006-03-23 17:38:03 -0800, Tim H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> Put the tests in a separate directory alongside the code. Chapter 12 of  
> the Pickaxe includes some “best practices” for unit testing Ruby with  
> Test::Unit.

I have a patch which fixes the test functionality in Minero A.'s  
setup.rb, which lets you create a test/ directory and run:

ruby setup.rb test

and it will execute all the tests in the test/ directory.

Please email privately if you want the patch (I’ve already submitted it  
and recieved no response).
