Testing Private methods

131 views
Skip to first unread message

Lloyd

unread,
Nov 1, 2016, 1:51:48 AM11/1/16
to CATCH
Hi,


What is the right way to test private member functions of a class? 

I read about private member testing and the solutions I got are, making the test class a friend or move the private members of the class to another class with public interface. 

In my case, I prefer the friend approach. Is it possible with Catch? How can I do this? Or is there some better approach using Catch?

Thanks,
  Lloyd

Jens Alfke

unread,
Nov 1, 2016, 12:11:27 PM11/1/16
to Lloyd, CATCH

On Oct 31, 2016, at 10:51 PM, Lloyd <lloydk...@gmail.com> wrote:

In my case, I prefer the friend approach. Is it possible with Catch? How can I do this? Or is there some better approach using Catch?

I’ve done this by making the test fixture class a friend of the class to be tested, then overriding the method in the fixture as protected (and just calling the superclass method.) The latter step is necessary because TEST_CASE_METHOD creates an invisible subclass of the fixture class, and that subclass is not a friend of your class so it doesn’t get access to private methods.

—Jens

Ryan Gonzalez

unread,
Nov 1, 2016, 12:13:51 PM11/1/16
to Lloyd, CATCH

If you're extremely desperate and insane:

#define private public

--
Ryan (ライアン)
[ERROR]: Your autotools build scripts are 200 lines longer than your program. Something’s wrong.
http://kirbyfan64.github.io/


--
You received this message because you are subscribed to the Google Groups "CATCH" group.
To unsubscribe from this group and stop receiving emails from it, send an email to catch-forum+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lloyd

unread,
Nov 2, 2016, 6:33:56 AM11/2/16
to CATCH
Thank you Jens 
Reply all
Reply to author
Forward
0 new messages