One of the reasons this isn't working
is that TEST_GROUP(TestCode) does magic, and creates a class with
another name than TestCode. You will need to have a look at the
macros to see what it is really named, but I'm not sure if it will
always be named that or if that is subject to change (Bas?).
When it comes to testing private functions, I'm in general
sceptical. What you want to test is that the class behaves as it
should, so you test Func1, which would use Func2 (if not, there
are no need to have Func2). You are not really interested in the
low level detail of what Func2 really does, but you are interested
in that calling Func1 has the desired result. If that means
calling Func2, so be it, but my test don't care, you are basically
testing Func2 through Func1.
Adding tests on private functions are possible, but will generally
make you code harder to change, as you are "locking in" the
implemntation details of your class, and not your interface. The
only reason I have seen for doing it is to get legacy code under
test (Legacy code = code without test, definition stolen from
James Grenning).
If you really want to test private functions (done myself on
legacy code) you can do the following:
#define private public
#include "headerfile_of_class_you_want_to_test"
but as I said, if this is to get legacy code under test, you
should do some work and refactor the need for this away.
- Martin
On 01/28/14 09:00, shiva prasad wrote:
Hi all,
I started writing CppUTest for a X class. I want to test
the private member functions of the class.. How do i make my
test code to be friend of X class.
I tried the below code but it is not working, Can anyone
please let me know how to access the private functions..
Class X
{
friend class TestCode;
public:
void Func1();
private:
void Func2();
}
Unit test code :
TEST_GROUP(TestCode)
{
X xobj;
TEST_SETUP()
{
//
Initialize stuff
}
TEST_TEARDOWN()
{
//
Clear stuff
}
};
TEST(TestCode,TestCase_Name)
{
xobj.Func2();
--> this throws an error of inaccessible function.
}
Thank you for the help.
Regards
Shiva
--
You received this message because you are subscribed to the Google
Groups "cpputest" group.
To unsubscribe from this group and stop receiving emails from it,
send an email to cpputest+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.