# Basics to test Unit

**URL:** <https://www.ruby-forum.com/t/basics-to-test-unit/134656>\
**Category:** Ruby\
**Created:** [April 13, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/basics-to-test-unit/134656 "2008-04-13T15:55:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [April 13, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/basics-to-test-unit/134656/1 "2008-04-13T15:55:36Z")

</div>

=begin  
Can the Test unit be used to test methods or variables in other files?  
I’ve  
come across this reference,  
([http://clarkware.com/cgi/blosxom/2005/03/18](http://clarkware.com/cgi/blosxom/2005/03/18)),  
which seems to test snipets of code within the test itself. The idea is  
to  
test that the array x is not empty nd use the Test Unit to do it.  
=end

# foo.rb

x = `df`.grep(/^/dev/).sort # in order of dev name

# test\_foo.rb

require ‘test/unit’  
require ‘/path/to/foo.rb’

# test that the df cmd exists on the system that we are running it on

# output of cmd is put in array. this array should NOT be !!–empty–!!

class TESTX \< Test::Unit::TestCase  
def test\_no\_df  
assert(x.empty?, “df command not found”)  
end  
end

- jjm

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [April 13, 2008, 5:16pm UTC](https://www.ruby-forum.com/t/basics-to-test-unit/134656/2 "2008-04-13T17:16:15Z")

</div>

On Sun, Apr 13, 2008 at 9:54 AM, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =begin  
> Can the Test unit be used to test methods or variables in other files?  
> I’ve  
> come across this reference, ([http://clarkware.com/cgi/blosxom/2005/03/18](http://clarkware.com/cgi/blosxom/2005/03/18)),  
> which seems to test snipets of code within the test itself. The idea is to  
> test that the array x is not empty nd use the Test Unit to do it.  
> =end

In that article, Mike is testing Ruby’s String class in order to learn  
Ruby.  
(That’s an effective way to learn a language, by the way.) Since Ruby  
already defines that class, he only has to write tests. When you’re  
writing  
both the test and production code, you’ll almost always put them in two  
separate files. For convenience, though, you can put test and production  
code in the same file. You might do this, for example, when you want to  
try  
something quickly or when you want to include a small example in an  
article  
or blog post.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [April 15, 2008, 2:41am UTC](https://www.ruby-forum.com/t/basics-to-test-unit/134656/3 "2008-04-15T02:41:06Z")

</div>

Local variables defined in foo.rb will not be available in test\_foo.rb.

Mark
