MFC + GoogleTests in 10 Easy Steps

1,939 views
Skip to first unread message

Dr. Eck

unread,
Jan 11, 2010, 11:16:24 PM1/11/10
to Google C++ Testing Framework
I had to spend an entire afternoon getting googletest running with an
MFC app. In hopes that I can save someone else the frustration,
here's a cookbook procedure:

To get GoogleTest running with an MFC app under VS2008:
1. Compile the gtest lib file with the (Properties | C/C++ | Code
Generation | Runtime Library) set to “Multi-threaded Debug DLL (/MDd)”
2. Create an MFC application project called <your app>Test
3. In the Project Properties for the Debug configuration
a. Under C/C++ | General | Additional include directories, add
the root path for gtest and gtest/include
b. Under Linker | General | Additional library directories, add
the path to gtest.lib
c. Under Linker | Input | Additional dependencies, add gtest.lib
4. In the <your app>Test.cpp file, paste a copy of the code from the
Google Test Primer “Writing the main() function” section
5. Fix the first #include to point to your header file.
6. Replace “FooTest” with “<your app>Test”.
7. Rewrite at least one of the two “TEST_F” functions to test your
code
8. Move the two lines in the main() function to the end of the
InitInstance() function that the AppWizard created.
9. Delete the “return TRUE;” line that was supplied. (return
RUN_ALL_TESTS() replaces it.)
10. Put the line “int argc[] = {0};” just before the lines you
inserted and change the second parameter of InitGoogleTest to (char**)
0

You should now be able to compile and run the tests. The results will
be displayed in the output window of the debugger.

One other snag I hit was that I had to include resource.h in a file
that referred to a resource ID.

Please try this out. Once it is perfected, perhaps it can be made
into a FAQ.

Zhanyong Wan (λx.x x)

unread,
Jan 12, 2010, 1:55:22 AM1/12/10
to Dr. Eck, Google C++ Testing Framework
Thanks for sharing, Dr. Eck.

On Mon, Jan 11, 2010 at 8:16 PM, Dr. Eck <dr.e...@gmail.com> wrote:
> I had to spend an entire afternoon getting googletest running with an
> MFC app. In hopes that I can save someone else the frustration,
> here's a cookbook procedure:
>
> To get GoogleTest running with an MFC app under VS2008:
> 1. Compile the gtest lib file with the (Properties | C/C++ | Code
> Generation | Runtime Library) set to “Multi-threaded Debug DLL (/MDd)”
> 2. Create an MFC application project called <your app>Test
> 3. In the Project Properties for the Debug configuration
> a. Under C/C++ | General | Additional include directories, add
> the root path for gtest and gtest/include
> b. Under Linker | General | Additional library directories, add
> the path to gtest.lib
> c. Under Linker | Input | Additional dependencies, add gtest.lib

Instead of b and c, can you add a dependency to the gtest vcproj file?

> 4. In the <your app>Test.cpp file, paste a copy of the code from the
> Google Test Primer “Writing the main() function” section
> 5. Fix the first #include to point to your header file.
> 6. Replace “FooTest” with “<your app>Test”.
> 7. Rewrite at least one of the two “TEST_F” functions to test your
> code
> 8. Move the two lines in the main() function to the end of the
> InitInstance() function that the AppWizard created.
> 9. Delete the “return TRUE;” line that was supplied. (return
> RUN_ALL_TESTS() replaces it.)
> 10. Put the line “int argc[] = {0};” just before the lines you
> inserted and change the second parameter of InitGoogleTest to (char**)
> 0

Step 10 means you are not passing the command line arguments to gtest.
As a result, death tests and all gtest command line flags won't work.

> You should now be able to compile and run the tests. The results will
> be displayed in the output window of the debugger.
>
> One other snag I hit was that I had to include resource.h in a file
> that referred to a resource ID.
>
> Please try this out. Once it is perfected, perhaps it can be made
> into a FAQ.
>

--
Zhanyong

Dr. Eck

unread,
Jan 15, 2010, 2:56:55 PM1/15/10
to Google C++ Testing Framework
> >     b. Under Linker | General | Additional library directories, add
> > the path to gtest.lib
> >     c. Under Linker | Input | Additional dependencies, add gtest.lib
>
> Instead of b and c, can you add a dependency to the gtest vcproj file?

I tried deleting the items in b. & c. and adding a dependency on
gtest.vcproj, but it didn't work. I have never set up a dependency on
a vcproj file before, so this could be my error or a more basic
problem.

> > 10.     Put the line “int argc[] = {0};” just before the lines you
> > inserted and change the second parameter of  InitGoogleTest to (char**)
> > 0
>
> Step 10 means you are not passing the command line arguments to gtest.
> As a result, death tests and all gtest command line flags won't work.

Good point. When I wrote my procedure I had only read the
GoogleTestPrimer It doesn't mention flags or death tests, so I was
unaware of them.

Kevin Navero

unread,
Feb 23, 2015, 5:12:42 AM2/23/15
to googletes...@googlegroups.com
Hi,

I tried the steps and ran into problems with this part:


"You should now be able to compile and run the tests.  The results will
be displayed in the output window of the debugger."

I don't get any output resulting from the tests, however, the only solution
I've found so far was to switch from windows application to console
application in Visual Studios and add a source file with a main function
in it. Has there been any other solutions or clarifications for this?
Reply all
Reply to author
Forward
0 new messages