news 2026/5/26 13:48:33

GoogleTest 使用指南 |自定义断言

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
GoogleTest 使用指南 |自定义断言

GoogleTest 使用指南 |自定义断言

  • GoogleTest 使用指南 |自定义断言

GoogleTest 使用指南 |自定义断言

在某些测试场景中,内置断言可能无法满足特定需求。此时,开发者可以定义自定义断言,以实现更灵活和精准的测试验证。

test_custom_assertion.cpp:

#include"gtest/gtest.h"#include<iostream>#include<sstream>#include<string>#include<vector>namespace{structAddress{std::string city;std::string zip_code;};structUserProfile{intid;std::string name;intage;boolactive;Address address;std::vector<std::string>roles;};std::stringFormatRoles(conststd::vector<std::string>&roles){std::ostringstream stream;stream<<"[";for(std::size_t i=0;i<roles.size();++i){if(i>0){stream<<", ";}stream<<roles[i];}stream<<"]";returnstream.str();}::testing::AssertionResultUserProfileMatches(constUserProfile&actual,constUserProfile&expected){std::ostringstream failures;if(actual.id!=expected.id){failures<<"id: expected "<<expected.id<<", actual "<<actual.id<<"\n";}if(actual.name!=expected.name){failures<<"name: expected "<<expected.name<<", actual "<<actual.name<<"\n";}if(actual.age!=expected.age){failures<<"age: expected "<<expected.age<<", actual "<<actual.age<<"\n";}if(actual.active!=expected.active){failures<<"active: expected "<<expected.active<<", actual "<<actual.active<<"\n";}if(actual.address.city!=expected.address.city){failures<<"address.city: expected "<<expected.address.city<<", actual "<<actual.address.city<<"\n";}if(actual.address.zip_code!=expected.address.zip_code){failures<<"address.zip_code: expected "<<expected.address.zip_code<<", actual "<<actual.address.zip_code<<"\n";}if(actual.roles!=expected.roles){failures<<"roles: expected "<<FormatRoles(expected.roles)<<", actual "<<FormatRoles(actual.roles)<<"\n";}conststd::string message=failures.str();if(message.empty()){return::testing::AssertionSuccess();}return::testing::AssertionFailure()<<"UserProfile mismatch:\n"<<message;}boolContains(conststd::string&text,conststd::string&expected){returntext.find(expected)!=std::string::npos;}}// namespaceTEST(CustomAssertionTest,VerifiesComplexObjectProperties){constUserProfile actual{1001,"Alice",28,true,{"Shanghai","200000"},{"admin","editor"}};constUserProfile expected{1001,"Alice",28,true,{"Shanghai","200000"},{"admin","editor"}};EXPECT_TRUE(UserProfileMatches(actual,expected));}TEST(CustomAssertionTest,ReportsAllMismatchedProperties){constUserProfile actual{1002,"Bob",30,false,{"Beijing","100000"},{"viewer"}};constUserProfile expected{1001,"Alice",28,true,{"Shanghai","200000"},{"admin","editor"}};const::testing::AssertionResult result=UserProfileMatches(actual,expected);conststd::string message=result.message();std::cout<<message;EXPECT_FALSE(result);EXPECT_TRUE(Contains(message,"id: expected 1001, actual 1002"));EXPECT_TRUE(Contains(message,"name: expected Alice, actual Bob"));EXPECT_TRUE(Contains(message,"age: expected 28, actual 30"));EXPECT_TRUE(Contains(message,"address.city: expected Shanghai, actual Beijing"));EXPECT_TRUE(Contains(message,"roles: expected [admin, editor], actual [viewer]"));}TEST(CustomAssertionTest,DISABLED_ShowsGoogleTestFailureMessage){constUserProfile actual{1002,"Bob",30,false,{"Beijing","100000"},{"viewer"}};constUserProfile expected{1001,"Alice",28,true,{"Shanghai","200000"},{"admin","editor"}};EXPECT_TRUE(UserProfileMatches(actual,expected));}

这个例子定义了一个复杂对象:

UserProfile { id name age active address.city address.zip_code roles }

并定义了自定义断言:

::testing::AssertionResultUserProfileMatches(constUserProfile&actual,constUserProfile&expected)

它会一次性检查多个属性;如果不匹配,会把所有差异都写进失败信息。

运行测试,会打印 mismatch message:

自定义断言的应用场景:

  • 复杂对象验证:验证对象的多个属性是否符合预期。
  • 自定义逻辑验证:根据特定业务逻辑定义断言条件。
  • 增强错误信息:在断言失败时提供详细的错误信息,便于快速定位问题。
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/26 13:46:02

LLM技术如何革新渗透测试效率与深度

1. LLM技术如何重塑渗透测试行业格局在网络安全攻防演练现场&#xff0c;一位资深渗透测试工程师正面临棘手难题——目标系统采用了全新的中间件版本&#xff0c;所有已知漏洞利用脚本均告失效。传统方法需要数小时甚至数天的漏洞研究&#xff0c;但此刻他打开PentestGPT界面&a…

作者头像 李华
网站建设 2026/5/26 13:45:05

上下文窗口不够用?代码仓库级 RAG 方案让 AI 记住整个项目

系列导读 你现在看到的是《AI Coding 助手从原理到落地:10 篇工程实践指南》的第 5/10 篇,当前这篇会重点解决:给出将 RAG 落地到 AI 编码助手的完整方案,突破上下文长度瓶颈。 上一篇回顾:第 4 篇《提示词工程在 AI Coding 中的实战:如何让模型写出你想要的代码》主要…

作者头像 李华
网站建设 2026/5/26 13:43:35

上海大歌剧院工程综合布线解决方案分析报告

一、项目概况与核心需求上海大歌剧院作为国内顶尖、亚洲一流、世界知名的专业歌剧院&#xff0c;是上海打造亚洲演艺之都的重要载体&#xff0c;也是世博文化公园的收官之作。项目总建筑面积达5.3 万平方米&#xff08;约 79.5 亩&#xff09;&#xff0c;内设 3 个剧场共 4200…

作者头像 李华
网站建设 2026/5/26 13:43:09

基于预训练语言模型的日志异常检测:LogFiT原理与实践

1. 项目概述与核心思路在运维和系统安全领域&#xff0c;日志文件就像系统的“黑匣子”&#xff0c;记录了每一次心跳、每一次交互和每一次异常。面对每天TB级别的日志数据&#xff0c;传统的人工巡检早已力不从心&#xff0c;自动化异常检测成为了刚需。我接触过不少方案&…

作者头像 李华
网站建设 2026/5/26 13:42:34

git pull底层原理与安全实践:fetch+merge/rebase深度解析

1. 为什么“git pull”不是魔法&#xff0c;而是一把双刃剑——一个老手的十年血泪总结刚入行那会儿&#xff0c;我带的第一个实习生&#xff0c;每天早上雷打不动执行三件事&#xff1a;喝咖啡、看邮件、敲git pull。他觉得这行命令就像给手机充电一样自然&#xff0c;插上就完…

作者头像 李华
网站建设 2026/5/26 13:41:36

山东大学软件学院创新实训(六)

日期&#xff1a;2026 年 5 月 23 日一、本周工作概述完成内容✅ NPC 对话记忆系统&#xff08;短期记忆 长期记忆&#xff09;✅ NPC 情感状态机&#xff08;情绪动态变化&#xff09;✅ 对话质量评估与优化机制✅ NPC 自主推理与质疑能力✅ DM 主持人智能控场优化✅ LLM 响应…

作者头像 李华