欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

代码单元测试工具:gmock

发布时间:2023/12/15 编程问答 35 豆豆
生活随笔 收集整理的这篇文章主要介绍了 代码单元测试工具: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.

 

 

 

参考:

  • https://blog.csdn.net/weixin_33807284/article/details/85093811

  • https://blog.csdn.net/niceniuniu/article/details/65629401

  • http://coney.github.io/2015/05/mock-static-function-with-mockcpp/

  • https://www.cnblogs.com/jycboy/p/gmock_summary.html

  • MockCpp手册(中文)

 

 

 

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

总结

以上是生活随笔为你收集整理的代码单元测试工具:gmock的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。