brettsalyer
brettsalyer

Reputation: 53

Automatically Generate Google Mock Methods?

I'm fairly new to C++ and Unit testing and I'm learning to use Google Mock and Google Test right now to test some code that I'm working with. Instead of writing all of the Google Mock methods manually, is there a way to point Google Mock to your class and have it automatically generate all of the Google Mock methods for all of your functions?

Someone told me they think it should be possible, but as I'm new to this (Just started learning C++ a few weeks ago) I have no idea if this is possible.

Upvotes: 3

Views: 3357

Answers (3)

Ehsan Mosadeq
Ehsan Mosadeq

Reputation: 1

This repository I2Mock has provided a simple script for converting a C++ interface [pure abstract calss] to both Google and Turtle mocks. You just need to give it the address of your CPP header and the generated mock class will be created next to it.

Upvotes: 0

Zeks
Zeks

Reputation: 2375

Quoting the gmock documentation:

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

https://code.google.com/p/googlemock/wiki/ForDummies

So, you can just pass your header to a python script and it will output a ready to use mock implementation. From my experience, it's not always 100% correct, but fixes are relatively trivial so this solution is what I use myself.

Upvotes: 6

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

" Instead of writing all of the Gmock methods manually, is there a way to point Gmock to your class and have it automatically generate all of the Gmock methods for all of your functions?"

Well, what I'm doing most of the time is copying a line from the interface

 struct IFace {
     int doThefancyOperatiion(std::string s, int i) = 0;
 };

and change it to

 struct MockIface {
       MOCK_METHOD2(doThefancyOperatiion, int (std::string s, int i));
 };

Looks like it can be done with sed or any other fairly decent tool for text replacement. Not I'm aware of a particular one, that does this for you.

Upvotes: 1

Related Questions