[Q] Test::Unit

I’m trying to figure out how to ust Test::Unit. I’ve read the
documentation in:
http://testunit.talbott.ws/doc/index.html

But I’m no closer to understanding it than I was before. At the end of
the page, the author says that (s)he’d like to get some feedback. Whoever
the author might be, here is some feedback:

  1. Include your name and email so that it’s easier to send you feedback.
  2. Could you include an example of how I’d use Test::Unit?

Here is a simple case. Say I write a private method to add two numbers:

class MyClass
private
def add(a,b)
return a+b
end
end

And I want to - say - assert that the return value is larger than the
first input value. How can I use Test::Unit to make this assertion?

How can I use Test::Unit to assert in-between steps inside my methods?

Currently I have things like:

condition or raise “condition was not met”

How can I replace this sort of thing by test units?

Thanks.

···


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

I’m trying to figure out how to ust Test::Unit. I’ve read the
documentation in:
http://testunit.talbott.ws/doc/index.html

[…] here is some feedback:

  1. Include your name and email so that it’s easier to send you feedback.

On the web page that you reference, there is a section called “Contact
Information”. It gives an email for feedback: testunit@talbott.ws

:slight_smile:

  1. Could you include an example of how I’d use Test::Unit?

Here is a simple case. Say I write a private method to add two numbers:

class MyClass
private
def add(a,b)
return a+b
end
end

And I want to - say - assert that the return value is larger than the
first input value. How can I use Test::Unit to make this assertion?

The key to using Test::Unit is to declare a class derived from
Test::Unit::TestCase. In this derived class you define methods that
begin with “test_”. Inside the methods you can assert anything you
want. There are several versions of assert that you can use. The major
one I use all the time are:

assert condition, optional_message
assert_equal expected_value, actual_value

There are also assertions that work with regular expressions and
exceptions. See the following URL for the gory details …

http://testunit.talbott.ws/doc/classes/Test/Unit/Assertions.html

Ok, here’s an example using your assertion …

require ‘test/unit’
require ‘myclass’

class TestMyClass < Test::Unit::TestCase
def test_greater
myclass = MyClass.new
assert myclass.send(:add, 1,2) > 1, “Should be greater”
end
end

Note the funky use of “send” in the assertion. If the “add” method were
not private, I would have written the assertion as …

 assert myclass.add(1,2) > 1, "Should be greater"

To run the above snippet, put it in a file (e.g. testmyclass.rb) and run

ruby testmyclass.rb

That’s all. Test::Unit will automatically figure out where the test
classes are and automatically run them.

Does that help?

···

On Mon, 2003-02-17 at 16:54, Daniel Carrera wrote:


– Jim Weirich jweirich@one.net http://w3.one.net/~jweirich

“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)

I’m trying to figure out how to ust Test::Unit. I’ve read the
documentation in:
http://testunit.talbott.ws/doc/index.html

But I’m no closer to understanding it than I was before. At the end of
the page, the author says that (s)he’d like to get some feedback. Whoever
the author might be, here is some feedback:

  1. Include your name and email so that it’s easier to send you feedback.
  2. Could you include an example of how I’d use Test::Unit?

You mean you read the whole page and didn’t see the Feedback, Contact
Information or Author headings?

Here is a simple case. Say I write a private method to add two numbers:

class MyClass
private
def add(a,b)
return a+b
end
end

And I want to - say - assert that the return value is larger than the
first input value. How can I use Test::Unit to make this assertion?

You don’t; unit tests test the public interface. If this method wasn’t
private, you could test it something like:

require ‘test/unit’

class TC_MyClass < Test::Unit::TestCase
def test_add
my_obj = MyClass.new
assert_equal(5, my_obj(3,2), ‘Add should work just like Fixnum#+’)
end
end

How can I use Test::Unit to assert in-between steps inside my methods?

You don’t, that’s not what unit testing is about

Currently I have things like:

condition or raise “condition was not met”

assert_foo(expected, condition, message)

Read about the assert_* methods on the page above

···

Daniel Carrera (dcarrera@math.umd.edu) wrote:

How can I replace this sort of thing by test units?


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Here is a simple case. Say I write a private method to add two numbers:

Testing of private methods was discussed in the last week or so; this list
is hard to keep up with :slight_smile: You have to use 'send' to bypass the private
restriction.

class MyClass
  private
    def add(a,b)
        return a+b
    end
end

And I want to - say - assert that the return value is larger than the
first input value. How can I use Test::Unit to make this assertion?

require 'test/unit'
require 'myclass'

class MyTest < Test::Unit::TestCase
  def setup
    @foo = MyClass.new
  end
  def test_add
    result = @foo.send(:add,5,3)
    assert(result==8, "Oops! I can't add!")
  end
end

Of course you'd probably loop over different sets of operands for a more
comprehensive test (store the operands and the expected results in a
constant array, for example)

How can I use Test::Unit to assert in-between steps inside my methods?

I don't think you can; you treat your methods as black boxes, put data in,
and see if you get the right data out.

Currently I have things like:

condition or raise "condition was not met"

How can I replace this sort of thing by test units?

If your method raises exceptions, then they will be caught by the test
harness: try adding
    raise "bomb!" if a == 5
to the 'add' method above.

If these exceptions are just defensive programming, then you shouldn't need
to do any more, since you don't expect them to be triggered.

If you want your unit tester to check that an exception *is* actually raised
under specific conditions, then use a begin..rescue..else..end construct in
the test, and pass in data which causes those conditions.

Regards,

Brian.

···

On Tue, Feb 18, 2003 at 06:54:08AM +0900, Daniel Carrera wrote:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/64561

···

On Tue, Feb 18, 2003 at 06:54:08AM +0900, Daniel Carrera wrote:

I’m trying to figure out how to ust Test::Unit. I’ve read the
documentation in:
http://testunit.talbott.ws/doc/index.html

But I’m no closer to understanding it than I was before. At the end of
the page, the author says that (s)he’d like to get some feedback. Whoever
the author might be, here is some feedback:

  1. Include your name and email so that it’s easier to send you feedback.
  2. Could you include an example of how I’d use Test::Unit?

Here is a simple case. Say I write a private method to add two numbers:

class MyClass
private
def add(a,b)
return a+b
end
end

And I want to - say - assert that the return value is larger than the
first input value. How can I use Test::Unit to make this assertion?

How can I use Test::Unit to assert in-between steps inside my methods?


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

How many chunks could checkchunk check if checkchunk could check chunks?
– Alan Cox

Or,
assert_equal(8, result, “Oops! I can’t add!”)

The purpose of the more specific method is so that Test::Unit knows
what you’re actually asserting, so can print a sensible error message
(and thus you don’t need to provide one).

Gavin

···

On Tuesday, February 18, 2003, 9:31:46 AM, Brian wrote:

assert(result==8, "Oops! I can't add!")