日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

单元测试: gmock

發(fā)布時(shí)間:2023/12/15 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 单元测试: gmock 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Mock,更確切地說(shuō)應(yīng)該是Mock Object。當(dāng)我們?cè)趩卧獪y(cè)試、模塊的接口測(cè)試時(shí),當(dāng)這個(gè)模塊需要依賴(lài)另外一個(gè)/幾個(gè)類(lèi),而這時(shí)這些類(lèi)還沒(méi)有開(kāi)發(fā)好,這時(shí)我們就可以定義Mock對(duì)象來(lái)模擬那些類(lèi)的行為。

mock工具的其中一個(gè)非常重要的作用是指定函數(shù)的行為(模擬函數(shù)的行為)。可以對(duì)入?yún)⑦M(jìn)行校驗(yàn),對(duì)出參進(jìn)行設(shè)定,還可以指定函數(shù)的返回值。

?

?

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.

?

?

?

參考:

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

  • MockCpp手冊(cè)(中文)

?

?

?

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.?測(cè)試執(zhí)行

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.?說(shuō)明

4.1?MOCK_METHOD

????MOCK_METHOD1(set, int(int num));? ? //調(diào)用set方法,一個(gè)參數(shù)(int?num),返回int型

????MOCK_METHOD0(get, int());? ? ? ? ? ?//調(diào)用get方法,無(wú)參數(shù),返回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

?

?

?

?

?

?

?

總結(jié)

以上是生活随笔為你收集整理的单元测试: gmock的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。