Unit Testing Private Methods 
Author Message
 Unit Testing Private Methods

It seems that in order to be able to unit test a method it has to be
publicly accessible. Is there a way to use test/unit to test private methods
and instance variables?

Thanks,



Mon, 01 Aug 2005 06:20:25 GMT  
 Unit Testing Private Methods

Quote:

> It seems that in order to be able to unit test a method it has to be
> publicly accessible. Is there a way to use test/unit to test private methods
> and instance variables?

I used to believe that Unit Testing would consider things in a black-box
approach. Can somebody comment on that?

You can however invoke private methods by using send
   someobject.send(:somemethod, *args)

as for the instance variables, you could get them doing

   assert_whatever ...

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

*** PUBLIC flooding detected from erikyyy
<lewnie> THAT's an erik, pholx.... ;)
        -- Seen on #LinuxGER



Mon, 01 Aug 2005 07:16:22 GMT  
 Unit Testing Private Methods

Quote:

> It seems that in order to be able to unit test a method it has to be
> publicly accessible. Is there a way to use test/unit to test private methods
> and instance variables?

You can use instance_eval to subvert "private."  Though when I run
into this, I usually skip testing them.  After all, the private
methods do their job properly if all the public methods that use them
pass their tests.

class MyClass
  def private_method
    "private"
  end
  private :private_method
end

if $0 == __FILE__
  require 'test/unit'
  class TestMyClass < Test::Unit::TestCase
    def setup

    end
    def test_private_method

    end
  end
end

--
matt



Mon, 01 Aug 2005 07:41:34 GMT  
 Unit Testing Private Methods

Quote:


> > It seems that in order to be able to unit test a method it has to be
> > publicly accessible. Is there a way to use test/unit to test private methods
> > and instance variables?

> I used to believe that Unit Testing would consider things in a black-box
> approach. Can somebody comment on that?

Thinking on it again, I should have written
  "white-box approach testing only public methods"

Correct?

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

But what can you do with it?
        -- ubiquitous cry from Linux-user partner



Mon, 01 Aug 2005 18:00:29 GMT  
 Unit Testing Private Methods

Quote:

>> > It seems that in order to be able to unit test a method it has to be
>> > publicly accessible. Is there a way to use test/unit to test private methods
>> > and instance variables?

>> I used to believe that Unit Testing would consider things in a black-box
>> approach. Can somebody comment on that?
> Thinking on it again, I should have written
>   "white-box approach testing only public methods"

I try to only test public methods, but sometimes run into a situation
where testing private methods is good.  This is generally when one
public method calls several large, implementation-heavy private
methods.  In this case, the private methods are important, and it's
easier to test them directly to build some confidence.

In which case


does the job.

I like the fact that you can call private methods, but the code for
doing so sticks out.

Gavin



Mon, 01 Aug 2005 22:25:18 GMT  
 
 [ 5 post ] 

 Relevant Pages 

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

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

3. How private are private methods?

4. unit testing: method actions vs. results

5. Children of private compilation units

6. Private Child Unit

7. Private Children of Generic Units

8. Test order in Test::Unit

9. Test order in Test::Unit

10. Automating UI tests (was Re: Art of Unit Testing)

11. Unit testing data; What to test

12. Test Tool for Unit Tests?

 

 
Powered by phpBB® Forum Software