Friday, April 1, 2011

Setting up a C++ project with Google Mock/Google Test in Visual Studio 2008

Steps that I had to take to get a C++ project setup to use Google Mock and Google Test: (assumes that Google Mock and Google Test has already been downloaded and compiled)

  1. Add a New Project to the solution
    1. Right-click on Solution - Add > New Project
    2. Select Win32 Console Application
    3. Give it a name
    4. Click Ok
    5. Click Finish on the Wizard that pops up
  2. Change the Project Properties
    1. Change Configuration at the top to All Configurations
    2. Under Configuration Properties > General > Use of MFC
      • Change the selection to be Use MFC in a Shared DLL
    3. Under Configuration Properties > C/C++ > General > Additional Include Directories
      1. Add the path to the include folder for Google Mock
        • Ex. - ..\..\CoreThirdParty\gmock-1.5.0\include
        • (CoreThirdParty is a directory that houses Google Mock, Google Test, and other Third Party headers and libraries that our project depends on)
      2. Add the path to the include folder for Google Test
        • Ex. - ..\..\CoreThirdParty\gtest-1.5.0\include
    4. Under Configuration Properties > Linker > General > Additional Library Directories
      1. Add the path to the Release folder for Google Mock
        • Ex. ..\..\CoreThirdParty\gmock-1.5.0\msvc\Release
    5. Under Configuration Properties > Linker > Input > Additional Dependencies
      1. Add the libraries for Google Mock
        • gmock.lib
        • gmock_main.lib
    6. Under Configuration Properties > Build Events > Post-Build Event > Command Line
      1. Add the command to run the executable after the build is finished
        • "$(TargetDir)$(TargetFileName)" --gtest_filter=*.* --gtest_shuffle --gtest_output=xml
  3. Add a HookUpTest to the .cpp file
    1. Remove all of the code in the .cpp file that was created with the project
    2. Add the following code to create a HookUp Test
#include <gmock/gmock.h>

TEST(SimpleTest, HookUpTest)
{
    ASSERT_TRUE(true);
}

Build this project and hopefully the output will be:


1>Finished generating code
1>Embedding manifest...
1>Performing Post-Build Event...
1>Running main() from gmock_main.cc
1>[==========] Running 1 test from 1 test case.
1>[----------] Global test environment set-up.
1>[----------] 1 test from SimpleTest
1>[ RUN      ] SimpleTest.HookUpTest
1>[       OK ] SimpleTest.HookUpTest(0 ms)
1>[----------] 1 test from SimpleTest(0 ms total)
1>[----------] Global test environment tear-down
1>[==========] 1 test from 1 test case ran. (0 ms total)
1>[  PASSED  ] 1 test.

No comments:

Post a Comment