1. 下载GoogleTest代码
$ git clone https://github.com/google/googletest.git
2. cmake和源代码
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(MyProject)
# 添加 Google Test 作为外部项目
add_subdirectory(googletest)
# 添加你的项目源文件
add_executable(MyExecutable test_my_code.cpp)
target_link_libraries(
MyExecutable
GTest::gtest_main
)
test_my_code.cpp
#include <gtest/gtest.h>
int Factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
TEST(FactorialTest, HandlesZeroInput) {
EXPECT_EQ(Factorial(0), 1);
}
TEST(FactorialTest, HandlesPositiveInput) {
EXPECT_EQ(Factorial(1), 1);
EXPECT_EQ(Factorial(2), 2);
EXPECT_EQ(Factorial(3), 6);
EXPECT_EQ(Factorial(8), 40320);
}
3. build & run
$ mkdir build & cd build
$ cmake ..
$ make
$ ./MyExecutable
Running main() from googletest/googletest/src/gtest_main.cc
[==========] Running 2 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 2 tests from FactorialTest
[ RUN ] FactorialTest.HandlesZeroInput
[ OK ] FactorialTest.HandlesZeroInput (0 ms)
[ RUN ] FactorialTest.HandlesPositiveInput
[ OK ] FactorialTest.HandlesPositiveInput (0 ms)
[----------] 2 tests from FactorialTest (0 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 1 test suite ran. (2 ms total)
[ PASSED ] 2 tests.
4. gMock
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(MyProject)
# 添加 Google Test 作为外部项目
add_subdirectory(googletest)
# 添加你的项目源文件
add_executable(MyMockExample demo_mock.cpp)
target_link_libraries(MyMockExample GTest::gmock_main)
demo_mock.cpp
#include <iostream>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
// 被测类接口
class FooInterface {
public:
virtual ~FooInterface() {}
virtual int GetValue() const = 0;
};
// 模拟对象类,继承自 FooInterface
class MockFoo : public FooInterface {
public:
MOCK_METHOD(int, GetValue, (), (const, override));
};
// 测试用例
TEST(MyTest, TestGetValue) {
MockFoo mockFoo; // 创建模拟对象
EXPECT_CALL(mockFoo, GetValue()) // 定义模拟对象的行为
.WillOnce(testing::Return(42)); // 当 GetValue 被调用时,返回 42
// 执行测试代码,假设被测类在某处调用了 mockFoo.GetValue()
int result = mockFoo.GetValue();
// 验证方法是否按预期被调用
EXPECT_EQ(result, 42);
}
TEST(MyTest, TestMethodCallTimes) {
MockFoo mock; // 创建模拟对象
// 定义模拟对象的行为
EXPECT_CALL(mock, GetValue())
.Times(3) // 该方法期望被调用 3 次
.WillRepeatedly(testing::Return(42));
// 调用模拟对象的方法
int result1 = mock.GetValue();
int result2 = mock.GetValue();
int result3 = mock.GetValue();
int result4 = mock.GetValue();
// 验证方法是否按预期被调用
EXPECT_EQ(result1, 42);
EXPECT_EQ(result2, 42);
EXPECT_EQ(result3, 42);
}
TEST(MyTest, TestMethodWillOnce) {
MockFoo mock; // Create a mock object
// Set expectations for two consecutive calls to GetValue()
EXPECT_CALL(mock, GetValue())
.WillOnce(testing::Return(42)) // First call returns 42
.WillOnce(testing::Return(200)); // Second call returns 100
// Call the mock method twice
int result1 = mock.GetValue();
int result2 = mock.GetValue();
// Verify the results
EXPECT_EQ(result1, 42);
EXPECT_EQ(result2, 100);
}
int main(int argc, char** argv) {
testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
测试结果
$ ./MyMockExample
[==========] Running 3 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 3 tests from MyTest
[ RUN ] MyTest.TestGetValue
[ OK ] MyTest.TestGetValue (0 ms)
[ RUN ] MyTest.TestMethodCallTimes
/home/demo/mybolog/gt/demo_mock.cpp:35: Failure
Mock function called more times than expected - returning default value.
Function call: GetValue()
Returns: 0
Expected: to be called 3 times
Actual: called 4 times - over-saturated and active
[ FAILED ] MyTest.TestMethodCallTimes (0 ms)
[ RUN ] MyTest.TestMethodWillOnce
/home/demo/mybolog/gt/demo_mock.cpp:65: Failure
Expected equality of these values:
result2
Which is: 200
100
[ FAILED ] MyTest.TestMethodWillOnce (0 ms)
[----------] 3 tests from MyTest (1 ms total)
[----------] Global test environment tear-down
[==========] 3 tests from 1 test suite ran. (1 ms total)
[ PASSED ] 1 test.
[ FAILED ] 2 tests, listed below:
[ FAILED ] MyTest.TestMethodCallTimes
[ FAILED ] MyTest.TestMethodWillOnce
2 FAILED TESTS