单元测试: gmock

Mock,更确切地说应该是Mock Object。当我们在单元测试、模块的接口测试时,当这个模块需要依赖另外一个/几个类,而这时这些类还没有开发好,这时我们就可以定义Mock对象来模拟那些类的行为。

mock工具的其中一个非常重要的作用是指定函数的行为(模拟函数的行为)。可以对入参进行校验,对出参进行设定,还可以指定函数的返回值。

Google's framework for writing and using C++ mock classes on a variety of platforms (Linux, Mac OS X, Windows, Windows CE, Symbian, etc). Inspired by jMock, EasyMock, and Hamcrest, and designed with C++'s specifics in mind, it can help you derive better designs of your system and write better tests.

Google Mock:

  • provides a declarative syntax for defining mocks,

  • can easily define partial (hybrid) mocks, which are a cross of real and mock objects,

  • handles functions of arbitrary types and overloaded functions,

  • comes with a rich set of matchers for validating function arguments,

  • uses an intuitive syntax for controlling the behavior of a mock,

  • does automatic verification of expectations (no record-and-replay needed),

  • allows arbitrary (partial) ordering constraints on function calls to be expressed,

  • lets a user extend it by defining new matchers and actions.

  • does not use exceptions, and

  • is easy to learn and use.

Google Mock is not a testing framework itself. Instead, it needs a testing framework for writing tests. Google Mock works seamlessly with Google Test, but you can also use it with any C++ testing framework.

参考:

1. 代码 mock_test.cc

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

using namespace testing;

class A {
public:
    int set(int num) {
        value = num;
        return num;
    }

    int get() {
        return value;
    }

    int value;
};

class MockA : public A {
public:
    MOCK_METHOD1(set, int(int num));
    MOCK_METHOD0(get, int());

};

TEST(Atest, getnum)
{
    MockA m_A;
    int a = 10;
    EXPECT_CALL(m_A, set(_)).WillRepeatedly(Return(a));
    int k = m_A.set(200);
    EXPECT_EQ(10, k);
}


int main(int argc, char *argv[]) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

2. 编译

g++ mock_test.cc -lgtest -lgmock -lpthread -std=c++11

3. 测试执行

baoli@ubuntu:~/tools/gtest/mytest$ ./a.out
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from Atest
[ RUN      ] Atest.getnum
[       OK ] Atest.getnum (0 ms)
[----------] 1 test from Atest (0 ms total)

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

4. 说明

4.1 MOCK_METHOD

    MOCK_METHOD1(set, int(int num));    //调用set方法,一个参数(int num),返回int型

    MOCK_METHOD0(get, int());           //调用get方法,无参数,返回int型

4.2 EXPECT_CALL

This means EXPECT_CALL() should be read as expecting that a call will occur in the future, not

that a call has occurred. Why does Google Mock work like that? Well, specifying the expectation

beforehand allows Google Mock to report a violation as soon as it arises, when the context (stack

trace, etc) is still available. This makes debugging much easier

发布了170 篇原创文章 · 获赞 116 · 访问量 33万+

猜你喜欢

转载自blog.csdn.net/u012247418/article/details/103332952