gtest/gmock 簡單實例

一. gtest下載安裝

cd kln
git clone https://github.com/google/googletest.git
cd googletest
cmake
make && make install
?  googletest git:(master) ? make && make install
[ 12%] Building CXX object googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 25%] Linking CXX static library ../lib/libgtest.a
[ 25%] Built target gtest
[ 37%] Building CXX object googlemock/CMakeFiles/gmock.dir/src/gmock-all.cc.o
[ 50%] Linking CXX static library ../lib/libgmock.a
[ 50%] Built target gmock
[ 62%] Building CXX object googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.o
[ 75%] Linking CXX static library ../lib/libgmock_main.a
[ 75%] Built target gmock_main
[ 87%] Building CXX object googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
[100%] Linking CXX static library ../lib/libgtest_main.a
[100%] Built target gtest_main
[ 25%] Built target gtest
[ 50%] Built target gmock
[ 75%] Built target gmock_main
[100%] Built target gtest_main
Install the project...
-- Install configuration: ""
-- Up-to-date: /usr/local/include
...

二. gtest測試示例

make example
cd example

main.cpp

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

int sum(int a, int b) {
    return a+b;
}

TEST(sum, testSum) {
    EXPECT_EQ(5, sum(2, 3));    // 求合2+3=5
    EXPECT_NE(3, sum(3, 4));    // 求合3+4 != 3
}

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

編譯運行

?  first git:(master) ? g++ -o main main.cpp -lgtest -lpthread -std=c++11
?  first git:(master) ? ./main
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from sum
[ RUN      ] sum.testSum
[       OK ] sum.testSum (0 ms)
[----------] 1 test from sum (0 ms total)

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

三. gmock 測試示例

被測試的對象的接口

?  first git:(master) ? cat Foo.h
#ifndef _FOO_H_INCLUDED_
#define _FOO_H_INCLUDED_

class Foo {
public:
  virtual int count() = 0;
};

#endif

創(chuàng)建一個Mock文件,內容如下

?  first git:(master) ? ../../googlemock/scripts/generator/gmock_gen.py Foo.h Foo > MockFoo.h
?  first git:(master) ? cat MockFoo.h
class MockFoo : public Foo {
 public:
  MOCK_METHOD0(count,
      int());
};

Bar.h

?  first git:(master) ? cat Bar.h
#ifndef _BAR_H_INCLUDED_
#define _BAR_H_INCLUDED_

#include "Foo.h"

class Bar {
public:
  Bar(Foo *foo) : m_foo(foo) {
  }

  int count() {
    return m_foo->count();
  }

private:
  Foo *m_foo;
};

#endif

main.cpp

?  first git:(master) ? cat main.cpp
// gtest_sum.cpp
#include <iostream>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "Bar.h"
#include "MockFoo.h"

using namespace testing;

TEST(BarTest, mock_foo_in_bar) {
  MockFoo *mockFoo = new MockFoo();
  Bar *bar = new Bar(mockFoo);

  EXPECT_CALL(*mockFoo, count())
    .Times(3)
    .WillOnce(Return(3))
    .WillOnce(Return(6))
    .WillOnce(Return(5));

  EXPECT_EQ(bar->count(), 3);
  EXPECT_EQ(bar->count(), 6);
  EXPECT_EQ(bar->count(), 5);

  delete bar;
  delete mockFoo;
}

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

編譯運行

?  first git:(master) ? g++ -o main main.cpp -lgtest -lgmock -lpthread -std=c++11
?  first git:(master) ? ./main
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from BarTest
[ RUN      ] BarTest.mock_foo_in_bar
[       OK ] BarTest.mock_foo_in_bar (0 ms)
[----------] 1 test from BarTest (0 ms total)

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

故意出錯

?  first git:(master) ? cat main.cpp
// gtest_sum.cpp
#include <iostream>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "Bar.h"
#include "MockFoo.h"

using namespace testing;

TEST(BarTest, mock_foo_in_bar) {
  MockFoo *mockFoo = new MockFoo();
  Bar *bar = new Bar(mockFoo);

  EXPECT_CALL(*mockFoo, count())
    .Times(5)
    .WillOnce(Return(2))
    .WillOnce(Return(6))
    .WillOnce(Return(5));

  EXPECT_EQ(bar->count(), 3);
  EXPECT_EQ(bar->count(), 6);
  EXPECT_EQ(bar->count(), 5);

  delete bar;
  delete mockFoo;
}

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

編譯執(zhí)行后的報錯

?  first git:(master) ? g++ -o main main.cpp -lgtest -lgmock -lpthread -std=c++11
?  first git:(master) ? ./main
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from BarTest
[ RUN      ] BarTest.mock_foo_in_bar

GMOCK WARNING:
main.cpp:14: Too few actions specified in EXPECT_CALL(*mockFoo, count())...
Expected to be called 5 times, but has only 3 WillOnce()s.main.cpp:20: Failure
Expected equality of these values:
  bar->count()
    Which is: 2
  3
main.cpp:14: Failure
Actual function call count doesn't match EXPECT_CALL(*mockFoo, count())...
         Expected: to be called 5 times
           Actual: called 3 times - unsatisfied and active
[  FAILED  ] BarTest.mock_foo_in_bar (0 ms)
[----------] 1 test from BarTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (1 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] BarTest.mock_foo_in_bar

 1 FAILED TEST
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,229評論 0 3
  • Gtest Github使用 gtest(gmock) 方便我們編寫組織 c++ 單元測試。 編譯 lib 到 g...
    orientlu閱讀 4,455評論 0 1
  • Nginx 的配置文件使用的就是一門微型的編程語言,許多真實世界里的 Nginx 配置文件其實就是一個一個的小程序...
    SkTj閱讀 4,429評論 0 7
  • 西瓜視頻玩的是心跳,挑戰(zhàn)的是超強大腦!這兩天,同事們都在玩西瓜視頻的百萬英雄!我也不例外,湊個熱鬧!還能挑戰(zhàn)一下自...
    笑對人生_1977閱讀 107評論 0 0
  • 01 昨天堂哥結婚,眾多親朋好友幾乎都聚在了一起,共同見證著這一對新人牽手走進幸福神圣的婚姻殿堂。 當天晚上,離家...
    億葉知秋閱讀 908評論 12 21

友情鏈接更多精彩內容