New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: friend classes #1542
Comments
A class is created for every test, which name is If you want to access private members from more than one test, to avoid defining (and maintaining) a big list of friend classes, then it's better to make the test group class the friend class. The name of the test group class is In that case, however, you will have to define "accessor" methods in your test group to access the tested class' members, since test classes derive from the test group class, but "friendship" is not inherited. Example: class MyClassUnderTest
{
private:
void MyMethod();
friend class TEST_GROUP_CppUTestGroupMyClassUnderTest;
} TEST_GROUP( MyClassUnderTest )
{
void CallMyMethod( MyClassUnderTest &o )
{
o.MyMethod();
}
}
TEST( MyClassUnderTest, MyMethod )
{
MyClassUnderTest testObj;
CallMyMethod( testObj );
// ...
}
|
Excellent, thanks, I will try it. |
Hi I tried it but it didn't work. I think maybe my class name is slightly wrong My C++ code has multiple classes in multiple files, and I have multiple test files. Let's suppose I have a class, And suppose I have a file
In my C++ header file, (a) all the tests in Thanks! |
Hi
I've been using CppUTest and I'm finding it really helpful. Thanks for this tool!
I would like to be able to use it to run tests on some of my
private
C++ member functions. Short of making them allpublic
, is there some way I can make the test code afriend class
?e.g. if my class is
MyClass
, and the test file isMyClassTest.cpp
, is there somewhere aMyClassTest
object which I could declare as a friend class ofMyClass
?I realise opinion is divided on whether or not to run tests on private functions, but personally I think it's ok.
thank you
The text was updated successfully, but these errors were encountered: