[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Basics to test Unit

John Maclean

4/13/2008 1:54:00 PM

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

3 Answers

Phlip

4/13/2008 3:08:00 PM

0

John Maclean wrote:

> Can the Test unit be used to test methods or variables in other files?

You can write anything you want in a unit test, so long as...

- it either passes or will soon pass
- your colleagues like it
- it duplicates no other test

To satisfy the last one, after cloning a test, change the sample data
around!

> I've
> come across this reference, (http://clarkware.com/cgi/blosxom/...),
> 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.

You should edit a Rails program using an editor that runs all tests when you
hit one (1) button. (None of the editors I have tried do that yet, but I'm
still hopeful.) Run all tests after every few edits.

In that environment, when you ask a question about code, including raw Ruby
code, you ask it with a test. For example, in one of our projects, we needed
to write a new acts_as_foo directive. Those directives work by abusing your
class's metaclass, and we didn't know how to do it. So we wrote tests that
experimented with a new directive, acts_as_frog, and we extended a local
class, inside the tests. After perfecting the technique we copied it into
our program's real acts_as_foo, and kept going.

> class TESTX < Test::Unit::TestCase
> def test_no_df
> assert(x.empty?, "df command not found")
> end
> end

This programmer wants these tests to check the environment. Everyone does
that, all the time, as a side-effect of tests that look at business logic.
This one just wants the error diagnostic, "df command not found", to appear
in the failing test transcript, to verbally diagnose the situation. That's
better than just "false is not true"!

--
Phlip
http://www.oreilly.com/catalog/9780...
"Test Driven Ajax (on Rails)"
assert_xpath, assert_javascript, & assert_ajax


Craig Demyanovich

4/13/2008 3:15:00 PM

0

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

On Sun, Apr 13, 2008 at 9:54 AM, John Maclean <jayeola@gmail.com> 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/...),
> 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

Mark Van Holstyn

4/15/2008 12:40:00 AM

0

[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

On Sun, Apr 13, 2008 at 9:54 AM, John Maclean <jayeola@gmail.com> 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/...),
> 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
>
>


--
Mark Van Holstyn, Partner / Software Developer
mvanholstyn@mutuallyhuman.com, (616) 706-6842
Mutually Human Software, http://mutuall...