Visual Studio 2013 and gmock v1.7.0

综合技术 2015-01-23

gmock is a unit testing and mocking framework available from Google
. Getting it set up and working correctly with a VS 2013 project takes a little bit of ceremony, however--and the errors one can stumble upon are not always the most helpful.

Get gmock compiled

Download the latest version of gmock (here we use v1.7.0

  • Open up the Visual Studio solution ( gmock-1.7.0msvc2010gmock.sln
  • Right click gmock > Properties > C/C++ > Code Generation > Runtime Library > Multi-threaded Debug
  • Build the solution
  • Ensure that gmock-1.7.0msvc2010Debug
    contains both gmock.lib
    and gmock_main.lib
    . You will link these into your test project.

Configure a test project

  • Open your Visual Studio solution (or create a new one). You should not
    create a new project within gmock-1.7.0msvc2010gmock.sln
  • Add New Project > Visual C++ > Win32 > Win32 Console Application
    . Name the project whatever you like, but I suggest XXXXXTest
    , where XXXXX is the name of the project you are testing.
  • Right click on your newly created test project > Properties > Configuration Properties > VC++ Directories
  • Include Directories >
    Add the full path to your gmock include and gtest include (e.g. C:Usersjalospinosogmock-1.7.0gtestinclude
    and C:Usersjalospinosogmock-1.7.0include
    ). Note: of course, you will want to add the /include folder for your project under test as well. Add this project as a dependency to your test project.
  • Library Directories > Add the full path to your gmock/gtest artifacts (e.g.
  • Configuration properties > Linker > Input > Additional Dependencies
  • Add gmock_main.lib
    and gmock.lib
    . Note also that you may want to include the name of the project under test here.
  • C/C++ > Code Generation > Runtime Library > Multi-threaded Debug

Add a test

In your test project, create a new source file called HelloTest.cpp
with the following contents:

#include "gmock/gmock.h"

TEST(HelloTest, AssertsCorrectly) {
    int value = 42;
    ASSERT_EQ(42, value);

Compile your solution. In your output folder, you will see a new artifact with a name corresponding to your new test project. This executable is a console application that runs all of the tests in your source:

> MyTest.exe
Running main() from
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MyTest
[ RUN      ] HelloTest.AssertsCorrectly
[       OK ] HelloTest.AssertsCorrectly(0 ms)
[----------] 1 test from MyTest(0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 1 test.

责编内容 (源链)。感谢您的支持!


C#中匿名函数、委托delegate和Action、Func、Expression、还有Lambda... 以前一直迷迷糊糊的,现在总算搞明白。 Lambda表达式 Lamda表达式基本写法是()=>{ }; Lambda和方法一样都可以传入参...
Go Interfaces are Awesome In object-oriented programming, an “interface” is a description of the thing...
MFC/VS2013对话框属性(三) VS2013 debug运行结果如下 打开VS2013, 文件(F)—》新建(N)—》项目(P)… 其他默认即可。 ...
C++ for C Programmers: part 1 2018 is an exciting time to be a programmer, with so many good “full service”...
Visual Studio 2017 15.3 已发布,增加.NET Core 2 支持... 微软现已发布 Visual Studio 2017 (VS2017) 15.3 的正式版。虽然该版本的 VS2017 包含了几个显著的特色,但最令人关注的还是对...