Skip to content
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

Open
mattagape opened this issue Feb 23, 2022 · 3 comments
Open

Question: friend classes #1542

mattagape opened this issue Feb 23, 2022 · 3 comments

Comments

@mattagape
Copy link

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 all public, is there some way I can make the test code a friend class?

e.g. if my class is MyClass, and the test file is MyClassTest.cpp, is there somewhere a MyClassTest object which I could declare as a friend class of MyClass?

I realise opinion is divided on whether or not to run tests on private functions, but personally I think it's ok.

thank you

@jgonzalezdr
Copy link
Contributor

A class is created for every test, which name is TEST_<testGroup>_<testName>_Test, so you can use that as the friend class name.

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 TEST_GROUP_CppUTestGroup<testGroup> (yes, it has bit weird name).

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 );

    // ...
}

@mattagape
Copy link
Author

Excellent, thanks, I will try it.

@mattagape
Copy link
Author

mattagape commented Feb 24, 2022

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, MyClass, which I wish to test. It has some private member functions.

And suppose I have a file MyClassTest.cpp. Within that I have, for example:


TEST_GROUP(MyClass)
{
  MyClass* myClass;

  void setup()
  {
    myClass = new MyClass();
  }
  void teardown()
  {
    delete myClass;
  }
};


TEST(MyClass, basic_ops)
{
  bool success = myClass->a_private_function();
  CHECK_TRUE(SUCCESS)
  success = myClass->another_private_function();
  CHECK_TRUE(SUCCESS)
}

TEST(MyClass, more_ops)
{
  bool success = myClass->another_private_function();
  CHECK_TRUE(SUCCESS)
  success = myClass->a_public_function();
  CHECK_TRUE(SUCCESS)
}

In my C++ header file, MyClass.h, what is the exact friend class name I need for:

(a) all the tests in MyClassTest.cpp, i.e. the entire test group for MyClass,
and
(b) only the tests in basic_ops?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants