Basics to test Unit

=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),
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

On Sun, Apr 13, 2008 at 9:54 AM, John M. [email protected] 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),
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

Local variables defined in foo.rb will not be available in test_foo.rb.

Mark

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs