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 
#include 

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

创建一个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 
#include 
#include 
#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 
#include 
#include 
#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();
}

编译执行后的报错

➜  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

你可能感兴趣的:(gtest/gmock 简单实例)