c++使用googletest进行单元测试

googletest进行单元测试

使用Google test进行测试

使用场景:

在平时写代码中,我们需要测试某个函数是否正确时可以使用Google test使用,当然,我们也可以自己写函数进行验证,但是使用google test是一个封装好的,使用也很简单,同时显示效果更佳能有效提高我们的效率

安装方式:

  1. 使用vcpkg: vcpkg install gtest:x64-windows
  2. 官网下载:传送门

vcpkg(c++包管理器)相关介绍,使用很简单:vcpkg 在clion和vs中的使用

以下是基于clion和vcpkg使用(vs好像继承了google test):

这里就不介绍怎么安装了,不会在clion中使用vcpkg安装gtest的可以看一看前面的文章,很快就能看完

一、单元测试

CMakeLists.txt文件

cmake 复制代码
cmake_minimum_required(VERSION 3.28)
# vcpkg经典模式下建议手动添加这个,不然可能会报错找不到路径,别折腾了
set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake"
        CACHE STRING "Vcpkg toolchain file")

project(test)

set(CMAKE_CXX_STANDARD 17)
find_package(GTest CONFIG REQUIRED)

add_executable(test main.cpp)

target_link_libraries(test PRIVATE GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main)

源代码

c++ 复制代码
//使用google test进行单元测试
#include <gtest/gtest.h>
#include <iostream>

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

TEST(testCase1,should_3_when_1_add_2)
{
	EXPECT_EQ(3, add(1, 2));
}

int main(int argc, char **argv)
{
	std::cout << "hello world" << std::endl;
    testing::InitGoogleTest(&argc, argv);
	return RUN_ALL_TESTS();
}

测试结果:

解释:

  1. TEST(testCase1,should_3_when_1_add_2) testCase1为测试名称,should_3_when_1_add_2为常用的命名规范,让人们一看就知道要测试什么

  2. EXPECT_EQ(3, add(1, 2)); EXPECT_EQ为测试宏,用于对比函数结果和预期结果,如果结果相同则测试正确,不同则报错

    这种是固定写法,用于测试使用

c++ 复制代码
 int main(int argc, char **argv)
 {
     testing::InitGoogleTest(&argc, argv);
     return RUN_ALL_TESTS();
 }

错误示例:

二、使用gmock测试

gmock测试呢,就是在你将函数写出来了,但还没有实现定义,这是你可以使用gmock实现简单的测试。

使用gmock时需要**继承**要gmock的类,例如我需要gmock User类的get_name函数,可以这样使用

c++ 复制代码
//User类定义
#ifndef USER_H
#define USER_H
#include <iostream>
class User{
public:
  virtual std::string get_name() = 0;
};

#endif
c++ 复制代码
//MockUser类定义
#ifndef MOCKUSER_H
#define MOCKUSER_H

#include "User.h"
#include <gmock/gmock.h>
class MockUser: public User{
public:
  MOCK_METHOD(std::string, get_name, (), (override));
};

#endif
c++ 复制代码
//使用google test进行单元测试
#include <gtest/gtest.h>
#include <iostream>
#include "MockUser.h"
int add(int a, int b) {
	return a + b;
}

TEST(testCase1, should_3_when_1_add_2) {
	EXPECT_EQ(3, add(1, 2));
}

TEST(testCase2, test) {
	MockUser user;
	EXPECT_CALL(user, get_name()).Times(testing::AtLeast(3)).
	WillOnce(testing::Return("one")).
	WillOnce(testing::Return("two")).
	WillRepeatedly(testing::Return("other"));

	std::cout << user.get_name() << std::endl;
	std::cout << user.get_name() << std::endl;
	std::cout << user.get_name() << std::endl;
	std::cout << user.get_name() << std::endl;
	std::cout << user.get_name() << std::endl;
	std::cout << user.get_name() << std::endl;
}

int main(int argc, char **argv) {
	std::cout << "hello world" << std::endl;
	testing::InitGoogleTest(&argc, argv);
	return RUN_ALL_TESTS();
}

测试结果:

解释:

  1. Times(testing::AtLeast(3))设置至少执行三次,如果少于三次就错误
  2. WillOnce(testing::Return("one")). WillOnce表示执行一次,Return表示传入的参数,这里有两个WillOnce分别调式第一次调用和第二次调用分别传入one和two
  3. WillRepeatedly(testing::Return("other")); 表示递归调用,这里的Return表示后续递归调用传入的值,前两次输出one和two,后面输出other
相关推荐
网络毒刘3 天前
端到端:用 Cursor Agent 完成「小功能 + 单测 + PR 描述」并附人工验收清单
单元测试·agent·ai编程·cursor·工具实践
mont evergreen3 天前
RTMP 连接单元测试之旅
单元测试·音视频·源码解读
我就是不信4 天前
CuTest:轻量级 C 语言单元测试框架实战指南
c语言·单元测试·log4j
虫无涯8 天前
Coverity 如何结合 GJB8114-2013使用?
git·单元测试·嵌入式测试·coverity·静态扫描
茉莉玫瑰花茶10 天前
GO [ 单元测试 ]
golang·单元测试·log4j
测试员周周11 天前
【jev应用到测试】94 条 UI 报错交给 Jev,三档判对 85.1%,开单闸门定在 0.6
人工智能·python·测试工具·单元测试·测试用例
ebiobiz13 天前
STM32F4 Unity 单元测试与多次测试失败率统计详细步骤
stm32·unity·单元测试
Glitch40414 天前
LLM 数据管线缺陷实录:两个单元测试无法覆盖的运行时问题
单元测试
钱栈up14 天前
全量 21 个失败、单跑全绿:泄漏进连接池的 SQL 变量
sql·单元测试·测试
冬奇Lab14 天前
LLM 自动化测试系列(03):单元测试生成——TestGen-LLM 与 Qodo Cover
人工智能·单元测试·测试