Basics to test Unit

J

John Maclean

=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
 
C

Craig Demyanovich

[Note: parts of this message were removed to make it a legal post.]

=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
 
M

Mark Van Holstyn

[Note: parts of this message were removed to make it a legal post.]

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

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,129
Latest member
FastBurnketo
Top