Test::Unit 
Author Message
 Test::Unit

I'm trying to figure out how to ust Test::Unit.  I've read the
documentation in:
http://www.*-*-*.com/

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



Sat, 06 Aug 2005 05:54:08 GMT  
 Test::Unit

Quote:
> -----Original Message-----

> 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.

Eh?  I'm guessing you got testunit off the RAA, in which case you would have
seen that Nathaniel Talbott is the author.  His name and email address are
also in the README file.  It's ALSO in the index.html file, under "Contact
Information".

Quote:
> 2) Could you include an example of how I'd use Test::Unit?

class TC_Foo < Test::Unit::TestCase
   def setup

   end

   def test_bar

   end

   def teardown

   end
end

There are also some examples in the "examples" directory.

Regards,

Dan



Sat, 06 Aug 2005 06:03:55 GMT  
 Test::Unit

Quote:

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

> Eh?  I'm guessing you got testunit off the RAA, in which case you would have
> seen that Nathaniel Talbott is the author.  His name and email address are
> also in the README file.  It's ALSO in the index.html file, under "Contact
> Information".

No.  As I said in my email, I was looking at:

http://testunit.talbott.ws/doc/index.html

Now that you mention it, I could have tried RAA.  But my suggestion of
including a name and email in the documentation above is still good.

Quote:
> class TC_Foo < Test::Unit::TestCase
[snip]
>    def test_bar

>    end
[snip]
> end

Does that mean that it's only possible to test the output of public
methods?

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



Sat, 06 Aug 2005 06:11:38 GMT  
 Test::Unit

Quote:

> [snip]
> Does that mean that it's only possible to test the output of public
> methods?

> [snip]

There's a thread on this started by Mauricio Fernandez that started
around February 12 and is titled "Unit Testing Private Methods". Enjoy!


Sat, 06 Aug 2005 06:24:03 GMT  
 Test::Unit

Quote:

> I'm trying to figure out how to ust Test::Unit.  I've read the
> documentation in:
> http://www.*-*-*.com/

> [...] 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

:-)

Quote:
> 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?

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{*filter*}details ...

    http://www.*-*-*.com/

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?

--

---------------------------------------------------------------------
"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)



Sat, 06 Aug 2005 06:24:40 GMT  
 Test::Unit

Quote:

> 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?

Quote:
> 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

Quote:
> 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

Quote:
> 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

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

--

All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E  7C11 332A 551C 796C 9F04

  application_pgp-signature_part
< 1K Download


Sat, 06 Aug 2005 06:29:53 GMT  
 Test::Unit

Quote:

> 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 :-) You have to use 'send' to bypass the private
restriction.

Quote:
> 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

  end
  def test_add

    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)

Quote:
> 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.

Quote:
> 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.



Sat, 06 Aug 2005 06:31:46 GMT  
 Test::Unit
Clearly the page is different when you look at it than when I do. When I
look at the page with that URL, there is a header called "Contact

I'm using Galeon 1.2.5, if that makes any difference.

Quote:


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

>> Eh?  I'm guessing you got testunit off the RAA, in which case you would
>> have seen that Nathaniel Talbott is the author.  His name and email
>> address are also in the README file.  It's ALSO in the index.html file,
>> under "Contact Information".

> No.  As I said in my email, I was looking at:

> http://testunit.talbott.ws/doc/index.html

> Now that you mention it, I could have tried RAA.  But my suggestion of
> including a name and email in the documentation above is still good.



Sat, 06 Aug 2005 07:15:58 GMT  
 Test::Unit

Quote:

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

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



Sat, 06 Aug 2005 11:03:00 GMT  
 Test::Unit

Quote:

> 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?

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

--
 _           _                            
| |__   __ _| |_ ___ _ __ ___   __ _ _ __  
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
        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



Sat, 06 Aug 2005 15:37:53 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Test::Unit::Mock: Mock objects for testing with Test::Unit

2. using test::unit for C++ unit tests

3. Test order in Test::Unit

4. Test order in Test::Unit

5. MSI-6000 disk test unit in Forth?

6. Test::Unit GUI

7. inifinity recursion && test/unit

8. Test::Unit 0.1.8 reporting problem

9. Test::Unit: assert in loop ?

10. Test::Unit::MockObject not working

11. Need help Translating Python unittest to Ruby Test::Unit

12. Test::Unit VERSION info

 

 
Powered by phpBB® Forum Software