C/C++测试框架googletest使用示例

发布于:2025-04-06 ⋅ 阅读:(14) ⋅ 点赞:(0)

文档

https://github.com/google/googletest
https://google.github.io/googletest/

编译安装

googletest是cmake项目,可以用cmake指令编译

cmake -B build && cmake --build build

将编译产物libinclude 两个文件夹复制到单独的文件夹,例如:/local/package/googletest

tree
.
├── include
│   └── gtest
│       ├── gtest-assertion-result.h
│       ├── gtest-death-test.h
│       ├── gtest-matchers.h
│       ├── gtest-message.h
│       ├── gtest-param-test.h
│       ├── gtest-printers.h
│       ├── gtest-spi.h
│       ├── gtest-test-part.h
│       ├── gtest-typed-test.h
│       ├── gtest.h
│       ├── gtest_pred_impl.h
│       ├── gtest_prod.h
│       └── internal
│           ├── custom
│           │   ├── README.md
│           │   ├── gtest-port.h
│           │   ├── gtest-printers.h
│           │   └── gtest.h
│           ├── gtest-death-test-internal.h
│           ├── gtest-filepath.h
│           ├── gtest-internal.h
│           ├── gtest-param-util.h
│           ├── gtest-port-arch.h
│           ├── gtest-port.h
│           ├── gtest-string.h
│           └── gtest-type-util.h
└── lib
    ├── libgmock.a
    ├── libgmock_main.a
    ├── libgtest.a
    └── libgtest_main.a

设置环境变量

# ~/.bashrc
# googletest env
GOOGLETEST_HOME=/local/package/googletest

# 静态库
export LIBRARY_PATH=$GOOGLETEST_HOME/lib:$LIBRARY_PATH

# include
export CPLUS_INCLUDE_PATH=$GOOGLETEST_HOME/include:$CPLUS_INCLUDE_PATH

示例

项目结构

.
├── CMakeLists.txt
├── include
│   └── math_util.h
├── src
│   ├── main.c
│   └── math_util.c
└── test
    ├── CMakeLists.txt
    ├── main.cpp
    └── math_util_test.cpp

4 directories, 7 files

test 是测试文件夹,使用cpp编写,src是代码库文件夹

./CMakeLists.txt

# cmake -B build && cmake --build build && ./build/App
cmake_minimum_required(VERSION 3.15)

project(App)

add_subdirectory(test)

include_directories("./include")

aux_source_directory("./src" DIR_SRCS)

add_executable(App ${DIR_SRCS})
add_library(APPLib STATIC ${DIR_SRCS})

enable_testing()
add_test(NAME test COMMAND test/AppTest)

./test/CMakeLists.txt

# cmake -B build && cmake --build build && ./build/AppTest

cmake_minimum_required(VERSION 3.15)

project(AppTest)

set(CMAKE_CXX_STANDARD 14)

# include
include_directories("../include")

# lib
link_libraries(gtest gtest_main APPLib)

# 编译文件
aux_source_directory(. TEST_SRC)

# 生成测试可执行程序
add_executable(AppTest ${TEST_SRC})

./test/math_util_test.cpp

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

extern "C"{
    #include "math_util.h"
}

TEST(AddTest, PositiveNos) {
    printf("PositiveNos");
    EXPECT_EQ(2, add(1, 1));
    EXPECT_EQ(10, add(5, 5));
    EXPECT_EQ(100, add(50, 50));
}

TEST(AddTest, NegativeNos) {
    printf("NegativeNos");
    EXPECT_EQ(-2, add(-1, -1));
    EXPECT_EQ(-10, add(-5, -5));
    EXPECT_EQ(-100, add(-50, -50));
    EXPECT_EQ(-20, add(-50, 30));
}


./test/main.cpp

#include <gtest/gtest.h>

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

./include/math_util.h

// math_util.h
#ifndef MATH_UTIL_H
#define MATH_UTIL_H
 
int add(int a, int b);
 
#endif

./src/math_util.c

#include "math_util.h"

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

./src/main.c

#include "math_util.h"
#include <stdio.h>

int main(int argc, char* argv[])
{
    int result = add(1, 1);
    printf("%d\n", result);
    return 0;
}

运行测试

cmake -B build && cmake --build build && ./build/test/AppTest

[==========] Running 2 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 2 tests from AddTest
[ RUN      ] AddTest.PositiveNos
test PositiveNos[       OK ] AddTest.PositiveNos (0 ms)
[ RUN      ] AddTest.NegativeNos
test NegativeNos[       OK ] AddTest.NegativeNos (0 ms)
[----------] 2 tests from AddTest (0 ms total)

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

完整代码:https://github.com/mouday/gtest-demo

参考文章