# Unit testing idiom

**URL:** <https://www.ruby-forum.com/t/unit-testing-idiom/106689>\
**Category:** Ruby\
**Created:** [June 13, 2007, 10:04am UTC](https://www.ruby-forum.com/t/unit-testing-idiom/106689 "2007-06-13T10:04:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [June 13, 2007, 10:04am UTC](https://www.ruby-forum.com/t/unit-testing-idiom/106689/1 "2007-06-13T10:04:19Z")

</div>

Hi all,

It’s always bothered me having code and unit tests in different places -  
keeping unit tests near the code they are testing _has_ to be a good  
thing, right?

Given the above, I stumbled on the following idiom:

$ cat testable.rb  
module Test; module Unit; class TestCase; end; end; end

module Testable  
def test\_case(&block)  
Class.new(Test::Unit::TestCase).module\_eval &block  
end  
end

$ cat builtin\_tests.rb  
require ‘testable’

class MyWorkingClass  
extend Testable

def foo(a,b)  
return a+b  
end

test\_case {  
def test\_foo\_null  
assert\_equal 2, MyWorkingClass.new.foo(1,1)  
end  
}  
end

if **FILE** == $0  
puts MyWorkingClass.new.foo(1,1)  
end

$ ruby builtin\_tests.rb  
2  
$ testrb builtin\_tests.rb  
Loaded suite builtin\_tests.rb  
Started  
.  
Finished in 0.00095 seconds.

1 tests, 1 assertions, 0 failures, 0 errors  
$

Just thought I’d share 🙂 Are there any drawbacks to this method that  
I haven’t seen yet?

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [June 13, 2007, 4:09pm UTC](https://www.ruby-forum.com/t/unit-testing-idiom/106689/2 "2007-06-13T16:09:17Z")

</div>

On 6/13/07, Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> It’s always bothered me having code and unit tests in different places -  
> keeping unit tests near the code they are testing _has_ to be a good  
> thing, right?

I don’t know that I agree with your premise. While it might seem  
convenient to combine tests with the artifacts they test, in general  
I think that separating them is a good thing.

Having separate tests acts as a sort of double-entry bookkeeping  
system. If they are combined there’s the possbility of ‘cooking the  
books’ either deliberately or unintentionally.

For simple things, such as trying something out, or demonstrating your  
ruby-foo on posts here, combining the tests and code being tested  
might be alright, but I think it’s an idea best used in moderation.  
For anything larger, I’d advocate for setting up a system for locating  
the test code, such as the directory structure used by Rails.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
