Is it needed to test a class's private method for Unit Test? (I think the answer is YES because Unit Test means testing all methods) If so, how to test a class's private method by CPPUnit? Thanks!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It's a common understanding in the xUnit crowd that if you need to test a private method of a particular class, it's likely that the method needs to be refactored out into it's own public method, or extracted to another class entirely.
Unit testing is a process to guarantee the public contract (public functions or methods) of a class that a client/caller will use.
If this were Java and you were writing a jUnit test, you could label the method default scope and test it directly in the same package. However, it's not and my recommendation without seeing what you're testing to consider the advice above.
Sorry to sound pedantic.
John
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
jdubchak is right -- you probably shouldn't do this. None the less, sometimes you need to be flexible. When debugging a public interface, for example, sometimes is is useful to isolate a private method for testing -- and once you have written the test, why throw it away?
I have done this before... It was the very first time I found use for the friend keyword. It is as wrong to use the friend keyword as it is to use a goto, but sometimes pragmatism needs to rule the day. I don't remember the details of how to use it properly, you will have to look it up, but it does work.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I meet the same problem.
Because the codes I want to test is done by other, so I can't don't want to change the private method to be public.
And at last, I make my class derived from the class I want to test. In this way,I can access the private member function, but I don't know whether it is proper.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi all,
Is it needed to test a class's private method for Unit Test? (I think the answer is YES because Unit Test means testing all methods) If so, how to test a class's private method by CPPUnit? Thanks!
It's a common understanding in the xUnit crowd that if you need to test a private method of a particular class, it's likely that the method needs to be refactored out into it's own public method, or extracted to another class entirely.
Unit testing is a process to guarantee the public contract (public functions or methods) of a class that a client/caller will use.
If this were Java and you were writing a jUnit test, you could label the method default scope and test it directly in the same package. However, it's not and my recommendation without seeing what you're testing to consider the advice above.
Sorry to sound pedantic.
John
jdubchak is right -- you probably shouldn't do this. None the less, sometimes you need to be flexible. When debugging a public interface, for example, sometimes is is useful to isolate a private method for testing -- and once you have written the test, why throw it away?
I have done this before... It was the very first time I found use for the friend keyword. It is as wrong to use the friend keyword as it is to use a goto, but sometimes pragmatism needs to rule the day. I don't remember the details of how to use it properly, you will have to look it up, but it does work.
I meet the same problem.
Because the codes I want to test is done by other, so I can't don't want to change the private method to be public.
And at last, I make my class derived from the class I want to test. In this way,I can access the private member function, but I don't know whether it is proper.
I think, you have to test a private method. Everything must be tested.
easy, you can define everything as a macro
so, example:
#define private public
#include <classyouwanttotestprivatemethod.h>
#undef private
But with this macro we can compile without any problem but cannot link. Any suggestions