当前位置: 首页 > news >正文

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

文章目录

    • 文档
    • 编译安装
    • 示例
    • 参考文章

文档

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

参考文章

http://www.dtcms.com/a/112705.html

相关文章:

  • 用HTML.CSS.JavaScript实现一个贪吃蛇小游戏
  • 基于Go语言实现一个网络聊天室(连接Redis版)
  • Kubernetes集群管理详解:从入门到精通
  • Eliet Chat开发日志:信令服务器注册与通信过程
  • JAVA单例模式
  • 2023-2024总结记录
  • leetcode二叉树刷题调试不方便的解决办法
  • 【Redis】服务端高并发分布式结构
  • 使用Scrapy官方开发的爬虫部署、运行、管理工具:Scrapyd
  • 网安小白筑基篇五:web后端基础之PHP
  • Springboot----@Role注解的作用
  • C++设计模式-解释器模式:从基本介绍,内部原理、应用场景、使用方法,常见问题和解决方案进行深度解析
  • 【Python使用】嘿马推荐系统全知识和项目开发教程第2篇:1.4 案例--基于协同过滤的电影推荐,1.5 推荐系统评估【附代码
  • 【Android】界面布局-线性布局LinearLayout-例子
  • 编程能力的跃迁时刻:技术革命与认知重构的交响曲
  • MySQL索引原理:从B+树手绘到EXPLAIN
  • 合肥京东运营服务商TOP5推荐
  • Axure数据可视化科技感大屏设计资料——赋能多领域,展示无限价值
  • C# 类库生成后自动复制到指定目录
  • Mysql 集群架构 vs 主从复制架构
  • PostgreSQL LIKE 操作符详解
  • 如何在windows 环境、且没有显卡的情况下用python跑通从ModelScope下载的大模型的调用
  • FPGA状态机思想实现流水灯及HDLBits学习
  • 02.unity各个面板说明
  • JSON Crack:简化数据可视化的参数编辑器
  • 【Guava】新集合 - BiMapMultimapMultiset
  • JavaScript中左键单击(click)与双击(dblclick)事件的关系解析地图操作避坑
  • vue项目data functions should return an object
  • Linux的 `/proc` 目录 笔记250404
  • 【kubernetes】BusyBox