如何在Visual Studio中使用GoogleMock? [英] How to use GoogleMock in Visual Studio?

查看:78
本文介绍了如何在Visual Studio中使用GoogleMock?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将是一个

最后的配置步骤是对三个Google .cc 文件禁用 Precompiled标头(重要:也请注意空白字段).

选项2:在静态库项目中使用GoogleTest


  1. 通过静态库(C ++)模板创建一个新项目.说明

    解决方案结构现在应如下所示:

    编写测试


    无论哪种方式,您现在都可以开始使用GoogleMock编写测试了.将 #include"gmock/gmock.h" 添加到 pch.h 文件:

     ////pch.h//标准系统的头文件包含文件.//#pragma一次#include"gtest/gtest.h"#include"gmock/gmock.h" 

    打开生成的 Test.cpp 文件并尝试.

      #include"pch.h";MockTest类{上市:MOCK_METHOD(void,SomeMethod,());};TEST(TestCaseName,TestName){MockTest模拟;EXPECT_CALL(mock,SomeMethod);模拟.SomeMethod();EXPECT_EQ(1,1);EXPECT_TRUE(true);} 

    This is going to be a self-answered, FAQ-style question. See answer below.

    With Visual Studio 2017/2019 it is really easy to set up a new Google Test project and start writing tests (as long as you don't mind using older versions of GoogleTest versions anyway).

    But what about using GoogleMock as well? You would think that since Google combined gtest/gmock some time ago that this would just work. Just #include "gmock/gmock.h" and mock away. But no, the GoogleTest NuGet package that is automatically added by the template does not include the gmock folder at all.

    Trying to add a second GoogleMock NuGet package causes multiple problems, such as mismatched gtest/gmock versions, overlapping include paths, etc.

    Replacing the Microsoft GoogleTest NuGet package with the one from Google causes a link error:

    MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
    

    So what is the current recommended (and least painful) way to set up GoogleTest/GoogleMock in Visual Studio? Tests should be able to be discovered, run, and debugged via the Test Explorer.

    解决方案

    I've found two ways to set this up: Either compile the whole GoogleTest framework directly into each of the test projects, or create a library project to hold it. Using a library will give faster build times, but you'll need to make sure that compile/link options are the same on the library and the test projects.

    Option 1: Compiling GoogleTest Directly in the Test Project


    1. Create a new project from the Google Test template. Instructions here if needed.
    2. Uninstall the Microsoft.googletest.v140.windesktop.msvcstl.static.rt-static NuGet package.
    3. Install the latest gmock NuGet package from Google (currently v1.10.0).
    4. Add the file gtest_main.cc to the project. It should be in ..\packages\gmock.1.10.0\lib\native\src\gtest\src\

    At this point the project should look something like this (if it doesn't, try Unloading and Reloading the project):

    The final configuration step is to disable use of Precompiled Headers for the three Google .cc files (Important: Notice the empty fields too).

    Option 2: Using GoogleTest in a Static Library Project


    1. Create a new project from the Static Library (C++) template. Instructions here if needed.
    2. Delete all generated .h/.cpp files (pch.h, pch.cpp, framework.h, <ProjectName>.cpp, etc)
    3. Install the latest gmock NuGet package from Google (currently v1.10.0).
    4. Disable use of Precompiled Headers for the library project (see related pic above).
    5. Create a new project from the Google Test template. Instructions here if needed.
    6. Uninstall the Microsoft.googletest.v140.windesktop.msvcstl.static.rt-static NuGet package.
    7. Add the file gtest_main.cc to the project. It should be in ..\packages\gmock.1.10.0\lib\native\src\gtest\src\
    8. Disable use of Precompiled Headers for gtest_main.cc (see related pic above).
    9. Add the library project to the test project's Project References.
    10. Add ..\packages\gmock.1.10.0\lib\native\include\ to the test project's Include Directories under VC++ Directories

    The solution structure should now look something like this:

    Writing the Tests


    Either way, you are now ready to start writing tests using GoogleMock. Add #include "gmock/gmock.h" to the pch.h file:

    //
    // pch.h
    // Header for standard system include files.
    //
    
    #pragma once
    
    #include "gtest/gtest.h"
    #include "gmock/gmock.h"
    

    Open the generated Test.cpp file and try it.

    #include "pch.h"
    
    class MockTest {
    public:
        MOCK_METHOD(void, SomeMethod, ());
    };
    
    TEST(TestCaseName, TestName) {
        MockTest mock;
        EXPECT_CALL(mock, SomeMethod);
        mock.SomeMethod();
        EXPECT_EQ(1, 1);
        EXPECT_TRUE(true);
    }
    

    这篇关于如何在Visual Studio中使用GoogleMock?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆