How to use gmock with MFC application

I've an unmanaged mfc application. I would like to integrate some unit testing for that using gmock. I added all the includes, libraries.

But how can I start running tests?

I know they use main method to run test in other cases.

#include "gmock/gmock.h"

int main(int argc, char** argv) {
    testing::InitGoogleMock(&argc, argv);
    return RUN_ALL_TESTS();
}

but since mfc applications don't have main method but InitInstance method. How can I start running test?

Upvotes: 0

Views: 575

Answers (1)

Lilshieste
Lilshieste

Reputation: 2754

Google recommends that you create a separate project for your tests - a test project - which has its own int main. Their FAQs page references the following resource for guidance:

http://hassanjamilahmad.blogspot.com/2009/07/gtest-starters-help.html

(This makes sense, if you think about it. There's no real reason you should be deploying your unit tests in your final product. Unit tests are meant for developers' consumption; not your customers'.)

In a nutshell, here is the general setup:

  • Create an empty C++ project and configure it to build an .exe
  • Define an int main in this project, and add the googletest boilerplate code that you mentioned
  • Specify that the test project is dependent on the output of your primary project (in the Linker properties)
  • Add a post-build event to this project that invokes its own output, thereby automatically running the tests

Some things to be wary of, based on past experience:

  • This setup requires classes/functions in your MFC project to be exported in order to be consumed by the test project (otherwise it has no way of accessing them)

  • We ran into some problems linking (i.e., via the linker) our MFC project to the test project. We ended up extracting code from the MFC project into separate libraries, and used googletest to test those libraries. (These were much easier to configure, and helped with code organization as a bonus.)

On Edit: To clarify, these details correspond to googletest but are also applicable to gmock, since gmock uses googletest under the hood.

Upvotes: 2

Related Questions