13

I am using gmock for unit testing C++ code. I am not using the gtest framework. I am using visual studio 2008's built-in testing framework.

Now my problem is that I have to manually write mock classes for a real class to unit test. For example if I have A class with 5 functions then I have to write MockAClass with 5 functions. Is there any way that these classes are automatically generated.

class AClass
{
public:
    virtual int AFunction()
    {
        return 5;
    }
    virtual int AFunctionWithArguments(int x)
    {
        return x;
    }



class MockAClass : public AClass
{
public:
    MOCK_METHOD0(AFucntion, int());
    MOCK_METHOD1(AFunctionWithArgument, int(int x));
};
vegemite4me
  • 6,621
  • 5
  • 53
  • 79
fhnaseer
  • 7,159
  • 16
  • 60
  • 112

2 Answers2

20

There is a tool bundled with the google mock project that will help you do this. However I think the tool requires python to be installed, and I don't know how well it works in a windows environment. I've also found that the generated files sometimes need a little tweak to work perfectly.

Here's the info from the docs:

Tip: If even this is too much work for you, you'll find the gmock_gen.py tool in Google Mock's scripts/generator/ directory (courtesy of the cppclean project) useful. This command-line tool requires that you have Python 2.4 installed. You give it a C++ file and the name of an abstract class defined in it, and it will print the definition of the mock class for you. Due to the complexity of the C++ language, this script may not always work, but it can be quite handy when it does. For more details, read the user documentation.

Here is new localization of this script.

Marek R
  • 32,568
  • 6
  • 55
  • 140
Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
3

Disclaimer: I used to work at Typemock

Have you considered using another tool?
Typemock has a tool called Isolator++ that do not need you to write "mocking classes".

TEST_F(IsolatorTests, SomeTest) 
{
      AClass* fakeclass = FAKE(AClass);
      WHEN_CALLED(fakeclass->AFunction()).Return(10);

      ASSERT_EQ(10, fakeclass.AFunction()); 
}

You can learn more about it at this about page

Dror Helper
  • 30,292
  • 15
  • 80
  • 129
  • i have read about isolator++. but that is not free. you have to get license for that. – fhnaseer Feb 13 '12 at 07:27
  • 3
    @DrorHelper Probably worth a disclaimer that you used to work for Typemock. (Not that that makes it a bad product etc, but its good to disclose such potential sources of bias). – Michael Anderson Feb 13 '12 at 07:48
  • 1
    Moq (http://code.google.com/p/moq/) is a free library for c# unit testing. isolator++ is same for c++ but they are asking money. i want a free library. dont have money to spent on that. i buy isolator++ for 600 dollars while the application i am making is of 200 dollar. doesnt make any sense to use isolator++. – fhnaseer Feb 13 '12 at 07:52
  • @Michael Anderson I would add the disclaimer although I don't see the relevnace - since I no longer work there – Dror Helper Feb 13 '12 at 08:08
  • @fhnaseer the sad truth is that usually it's a matter of trade-off either spend your money or your time (equals money). I wish I knew of a free (preferably open source) tool that can help you but I'm not aware of one. Another option is to use a manager C++ wrapper and use .NET (free) mocking framework but it seems like a lot of work. – Dror Helper Feb 13 '12 at 08:10
  • somebody told me that there are scripts which can do this, any idea about that, – fhnaseer Feb 13 '12 at 09:18