如何在CppUnitTestFramework中使用Google Mock

时间:2019-03-27 18:41:41

标签: c++ visual-studio unit-testing googlemock microsoft-cpp-unit-test

TL; DR: 您可以使用GMock将模拟功能添加到Microsoft本机c ++单元测试中。有关详细信息,请参见下面的答案。


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

Google的GMock框架似乎可以提供我所需的一切,文档显示它可以与gtest以外的框架一起使用。因此,利用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 screen snip

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

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

1 个答案:

答案 0 :(得分:0)

我最终能够使GMockCppUnitTestFramework正常工作。然后,我创建了一组简单的接口函数,以使其更易于使用。

我使用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 clear any previous failures and expectations.
        void ResetGoogleMock();

        // Call once per test method to check GoogleMock expectations.
        void CheckGoogleMock(void *mockObj=nullptr);
    }
}

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()
        {
            int argc = 0;
            char** argv = nullptr;
            ::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();
        }

        // Asserts if any expectations fail to verify on the Mock object
        void CheckGoogleMock(void *mockObj)
        {
            Assert::IsNotNull(_listener, L"Google Mock framework not initialized by InitGoogleMock()");
            bool result = true;
            if (mockObj)
                result = Mock::VerifyAndClearExpectations(mockObj);
            _listener->CheckFailures();
            Assert::IsTrue(result);
        }
    }
}

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

#include "GMockUtils.h"

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

namespace GMockUtilsDemo
{
    TEST_CLASS(GMockUtilTests)
    {
    public:
        MockTestClass _mockObj;

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

        TEST_METHOD_INITIALIZE(MethodInitializer)
        {
            // Clean up any left over expectations from failed tests
            GMockUtils::ResetGoogleMock();
        }

        TEST_METHOD_CLEANUP(MethodCleanup)
        {
            // Check that expectations were met. Asserts if not.
            GMockUtils::CheckGoogleMock(&_mockObj);
        }

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

        TEST_METHOD(Method1_ParamIsZero_IntentionallyFail)
        {
            // Expectation will not be met
            EXPECT_CALL(_mockObj, Method2(1)).Times(1);
            _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 Test Explorer运行测试,我还可以看到与特定测试有关的所有GMock消息。它还可以与Azure DevOps上的VsTest任务一起使用。

Test Explorer screen snippet

希望这对发现自己处于相同情况的任何人都是有用的。