如何使用 Google Mock 和 CppUnitTestFramework

How to use Google Mock with CppUnitTestFramework

TL;DR: 您可以使用 GMock 为您的 Microsoft 本机 c++ 单元测试添加模拟功能。详情见下文


我想开始向我现有的本机单元测试集添加模拟。这些测试是使用 Microsoft 的 CppUnitTestFramework 框架编写的,该框架不支持模拟。我真的不想为了添加一些模拟而将整个测试套件转换为另一个框架。

Google 的 GMock framework seems to provide everything I need and the documentation suggests it can be used with frameworks other than gtest. So using advice from blog posts like this one,我创建了几个单元测试。

    TEST_MODULE_INITIALIZE(ModuleInitialize)
    {
        // Enable google mock
        GTEST_FLAG(throw_on_failure) = true;
        int argc = 0;
        TCHAR **argv = nullptr;
        InitGoogleMock(&argc, argv);
    }

    TEST_CLASS(GMockTests)
    {
    public:
        MockTestClass _mockObj;

        TEST_METHOD(Method1_ParamIsOne_Method2CalledWithOne)
        {
            EXPECT_CALL(_mockObj, Method2(1)).Times(1);
            _mockObj.Method1(1);
        }

        TEST_METHOD(Method1_ParamIsZero_IntentionallyFail)
        {
            // Expectation will fail
            EXPECT_CALL(_mockObj, Method2(1)).Times(1);
            _mockObj.Method1(0);
        }

    };

结果不太令人满意。期望确实有效(第一个方法通过),但如果任何期望失败,整个 运行 将中止,测试输出中只有以下无用消息:

[3/27/2019 11:39:17 AM Error] The active test run was aborted. Reason: 
[3/27/2019 11:39:17 AM Informational] ========== Run test finished: 0 run (0:00:22.3042194) ==========

Visual Studio Test Explorer window 也没有指出问题所在。它只是表明一个测试成功而另一个没有 运行:

所以我从这个集成中寻找的是:

  1. GMock 测试失败不会中止整个 运行。
  2. 未通过 GMock 预期的测试显示为失败。
  3. 所有 GMock 消息都应显示在测试输出中。

我能够通过创建自定义 TestEventListener 获得 GMock working correctly with the CppUnitTestFramework。然后我创建了一组简单的接口函数,使其更易于使用。

我使用gmock 1.7.0 NuGet package将GMock框架安装到我的项目中,并添加了如下所示的两个文件。

GMockUtils.h

#pragma once

#include <CppUnitTest.h>
#include <gmock/gmock.h>

namespace testing { 
    namespace GMockUtils {

        // Call once per test class or module to set up everything needed by GoogleMock.
        void InitGoogleMock();

        // Call once per test method to check for GoogleMock messages.
        void CheckGoogleMock();
    }
}

GMockUtils.cpp

#include "stdafx.h"
#include "GMockUtils.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace testing {
    namespace GMockUtils {
        namespace {

            // Test event listener for use with CppUnitTestFramework
            class CppUnitTestReporter : public EmptyTestEventListener
            {
            public:
                CppUnitTestReporter() : _failed(false) 
                {
                }

                // Helper for converting std::string to std::wstring
                std::wstring to_wstring(const std::string& str) const
                {
                    std::wstring output;
                    std::copy(str.begin(), str.end(), std::back_inserter(output));
                    return output;
                }

                // Called after a failed assertion or a SUCCEED() invocation.
                void OnTestPartResult(const ::testing::TestPartResult& result) override
                {
                    // Log this result to the CppUnitTestFramework output
                    Logger::WriteMessage(result.summary());

                    // Note: You cannot do an Assert directly from a listener, so we
                    // just store the messages until CheckGoogleMock() is called.
                    if (result.failed())
                    {
                        _failed = true;

                        // Append this result to the running summary
                        _failedSummary += result.message();
                        _failedSummary += "\n";
                    }
                }

                // Clear any previous failures
                void ResetFailures()
                {
                    _failed = false;
                    _failedSummary.clear();
                }

                // Assert if any failures have been detected. Also resets failures.
                void CheckFailures()
                {
                    auto failed = _failed;
                    auto failedSummary = _failedSummary;
                    ResetFailures();
                    Assert::IsFalse(failed, to_wstring(failedSummary).c_str());
                }

            protected:
                bool _failed;
                std::string _failedSummary;

            } *_listener;
        }

        // Initialize the Google Mock framework for use with CppUnitTestFramework
        void InitGoogleMock()
        {
            // Avoids calling the function unnecessarily
            if (_listener != nullptr)
                return;

            // Dummy command line parameters (could pass exe path here)
            int argc = 0;
            char** argv = nullptr;

            // Initialize the framework
            ::testing::InitGoogleMock(&argc, argv);

            // We don't want exceptions thrown, regardless what the doc says
            GTEST_FLAG(throw_on_failure) = false;

            // Remove default listener
            auto &listeners = UnitTest::GetInstance()->listeners();
            delete listeners.Release(listeners.default_result_printer());

            // Create and install the new listener
            _listener = new CppUnitTestReporter();
            listeners.Append(_listener);
        }

        // Reset any previous failures detected by the listener
        void ResetGoogleMock()
        {
            _listener->ResetFailures();
        }

        // Prints messages and asserts if any Google Mock messages are found.
        void CheckGoogleMock()
        {
            Assert::IsNotNull(_listener, L"Google Mock framework not initialized by InitGoogleMock()");
            _listener->CheckFailures();
        }
    }
}

我在单元测试中使用 GMockUtils 函数 类 是这样的:

#include "GMockUtils.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace ::testing;

namespace GMockUtilsDemo
{
    TEST_CLASS(GMockUtilTests)
    {
    public:

        TEST_CLASS_INITIALIZE(ClassInitializer)
        {
            // IMPORTANT: This must be called before any mock object constructors
            GMockUtils::InitGoogleMock();
        }

        TEST_METHOD_CLEANUP(MethodCleanup)
        {
            // Checks for GoogleMock messages. Asserts if found.
            GMockUtils::CheckGoogleMock();
        }

        TEST_METHOD(Method1_ParamIsOne_Method2CalledWithOne)
        {
            MockTestClass mockObj;
            EXPECT_CALL(mockObj, Method2(1)).Times(1); // Expectation will be met
            mockObj.Method1(1);
        }

        TEST_METHOD(Method1_ParamIsZero_IntentionallyFail)
        {
            MockTestClass mockObj;
            EXPECT_CALL(mockObj, Method2(1)).Times(1); // Expectation will not be met
            mockObj.Method1(0);
        }
    };
}


控制台输出

控制台输出现在显示所有 GMock 消息,运行 不会在第一次测试失败时中止。

[3/27/2019 12:23:46 PM Informational] ------ Run test started ------
[3/27/2019 12:23:46 PM Informational] 
Unexpected mock function call - returning directly.
    Function call: Method2(0)
Google Mock tried the following 1 expectation, but it didn't match:

c:\...\gmockutilsdemo.cpp(64): EXPECT_CALL(_mockObj, Method2(1))...
  Expected arg #0: is equal to 1
           Actual: 0
         Expected: to be called once
           Actual: never called - unsatisfied and active
[3/27/2019 12:23:46 PM Informational] Actual function call count doesn't match EXPECT_CALL(_mockObj, Method2(1))...
         Expected: to be called once
           Actual: never called - unsatisfied and active
[3/27/2019 12:23:46 PM Informational] ========== Run test finished: 2 run (0:00:00.8631468) ==========


测试资源管理器视图

如果我通过 Visual Studio 测试资源管理器 运行 测试,我还可以看到与特定测试相关的所有 GMock 消息。它还适用于 Azure DevOps 上的 VsTest 任务。

希望这对遇到相同情况的任何人都有用。