CPPTest实例分析(C++ Test)

news/2024/5/20 2:39:10

1 概述

  CppTest是一个可移植、功能强大但简单的单元测试框架,用于处理C++中的自动化测试。重点在于可用性和可扩展性。支持多种输出格式,并且可以轻松添加新的输出格式。

CppTest下载地址:下载地址1  下载地址2

下面结合实例分析下CppTest如何使用。

2 实例

利用CppTest编写单元测试用例需要从Suite类派生(这里Suite翻译为组),CppTest所有类型命名空间是Test。
实例选择CppTest源码中自带的例子。

2.1 无条件失败测试用例

测试用例组定义如下:

#include "cpptest.h"// Tests unconditional fail asserts
//
class FailTestSuite : public Test::Suite
{
public:FailTestSuite(){TEST_ADD(FailTestSuite::success)TEST_ADD(FailTestSuite::always_fail)}
private:void success() {}void always_fail(){// This will always fail//TEST_FAIL("unconditional fail");}
};

说明:

  • 类型FailTestSuite从Test::Suite派生
  • 在构造函数中通过宏TEST_ADD增加两个测试用例success和always_fail
  • success函数什么也不作做所以是成功的
  • always_fail函数调用TEST_FAIL宏报告一个无条件失败。

2.2 比较测试用例

测试用例组定义如下:

class CompareTestSuite : public Test::Suite
{
public:CompareTestSuite(){TEST_ADD(CompareTestSuite::success)TEST_ADD(CompareTestSuite::compare)TEST_ADD(CompareTestSuite::delta_compare)}
private:void success() {}void compare(){// Will succeed since the expression evaluates to true//TEST_ASSERT(1 + 1 == 2)// Will fail since the expression evaluates to false//TEST_ASSERT(0 == 1);}void delta_compare(){// Will succeed since the expression evaluates to true//TEST_ASSERT_DELTA(0.5, 0.7, 0.3);// Will fail since the expression evaluates to false//TEST_ASSERT_DELTA(0.5, 0.7, 0.1);}
};

说明:

  • 类型CompareTestSuite从Test::Suite派生
  • 在构造函数中通过宏TEST_ADD增加三个测试用例success, compare和delta_compare
  • success函数什么也不作做所以是成功的
  • compare函数调用TEST_ASSERT宏判断条件是否成立,如果条件失败报告错误。
  • delta_compare函数调用TEST_ASSERT_DELTA宏判断条件是否成立,第一次调用满足0.7 > (0.5 - 0.3) && 0.7 < (0.5 + 0.3)所以是成功的,第二次调用不满足0.7 > (0.5 - 0.1) && 0.7 < (0.5 + 0.1)所以报告失败。

2.3 异常测试用例

测试用例组定义如下:

class ThrowTestSuite : public Test::Suite
{
public:ThrowTestSuite(){TEST_ADD(ThrowTestSuite::success)TEST_ADD(ThrowTestSuite::test_throw)}
private:void success() {}void test_throw(){// Will fail since the none of the functions throws anything//TEST_THROWS_MSG(func(), int, "func() does not throw, expected int exception")TEST_THROWS_MSG(func_no_throw(), int, "func_no_throw() does not throw, expected int exception")TEST_THROWS_ANYTHING_MSG(func(), "func() does not throw, expected any exception")TEST_THROWS_ANYTHING_MSG(func_no_throw(), "func_no_throw() does not throw, expected any exception")// Will succeed since none of the functions throws anything//TEST_THROWS_NOTHING(func())TEST_THROWS_NOTHING(func_no_throw())// Will succeed since func_throw_int() throws an int//TEST_THROWS(func_throw_int(), int)TEST_THROWS_ANYTHING(func_throw_int())// Will fail since func_throw_int() throws an int (not a float)//TEST_THROWS_MSG(func_throw_int(), float, "func_throw_int() throws an int, expected a float exception")TEST_THROWS_NOTHING_MSG(func_throw_int(), "func_throw_int() throws an int, expected no exception at all")}void func() {}void func_no_throw() {}void func_throw_int() { throw 13; }
};

说明:

  • 类型ThrowTestSuite从Test::Suite派生
  • 在构造函数中通过宏TEST_ADD增加两个测试用例success和test_throw
  • success函数什么也不作做所以是成功的
  • 函数func和func_no_throw不会抛异常
  • 函数func_throw_int抛int类型异常13
  • test_throw调用6种宏测试函数调用异常,_MSG后缀版本指定异常文本。
    • TEST_THROWS_MSG 宏测试函数调用如果抛出指定异常则成功,否则失败
    • TEST_THROWS 宏测试函数调用如果抛出指定异常则成功,否则失败
    • TEST_THROWS_ANYTHING_MSG 宏测试函数调用如果抛出任意类型异常则成功,否则失败
    • TEST_THROWS_ANYTHING 宏测试函数调用如果抛出任意类型异常则成功,否则失败
    • TEST_THROWS_NOTHING 宏测试函数调用不抛异常则成功,否则失败
    • TEST_THROWS_NOTHING_MSG 宏测试函数调用不抛异常则成功,否则失败

2.4 测试用例运行

前面定义了三个测试用例组FailTestSuite,CompareTestSuite和ThrowTestSuite,下面将三个测试用例组加到测试程序中。

2.4.1 main

main(int argc, char* argv[])
{try{// Demonstrates the ability to use multiple test suites//Test::Suite ts;ts.add(unique_ptr<Test::Suite>(new FailTestSuite));ts.add(unique_ptr<Test::Suite>(new CompareTestSuite));ts.add(unique_ptr<Test::Suite>(new ThrowTestSuite));// Run the tests//unique_ptr<Test::Output> output(cmdline(argc, argv));ts.run(*output, true);Test::HtmlOutput* const html = dynamic_cast<Test::HtmlOutput*>(output.get());if (html)html->generate(cout, true, "MyTest");}catch (...){cout << "unexpected exception encountered\n";return EXIT_FAILURE;}return EXIT_SUCCESS;
}

说明:

  • 定义测试用例组ts
  • 通过ts函数add将FailTestSuite,CompareTestSuite和ThrowTestSuite加到测试用例组ts中
  • 定义测试输出对象outuput
  • 调用ts函数run运行测试用例,run第二参数cont_after_fail指示出错后是否接着执行,这里设置为true表示出错后接着执行。
  • 如果output是html类型,最后将html内容输出标准输出cout.

2.4.2 usage/cmdline

CppTest的输出格式默认支持四种格式:

  • Compiler 编译器格式
  • Html 网页格式
  • TextTerse 简约文本格式
  • TextVerbose 详细文本格式

可以派生新的输出格式:

  • 从Test::Output类型派生新的输出格式。
  • 从Test::CollectorOutput类型派生新的收集器输出格式。收集器输出格式整个测试用例运行完毕后再输出,HtmlOutput就是收集器输出格式。

下面代码从命令参数获取输出格式:

static void
usage()
{cout << "usage: mytest [MODE]\n"<< "where MODE may be one of:\n"<< "  --compiler\n"<< "  --html\n"<< "  --text-terse (default)\n"<< "  --text-verbose\n";exit(0);
}static unique_ptr<Test::Output>
cmdline(int argc, char* argv[])
{if (argc > 2)usage(); // will not returnTest::Output* output = 0;if (argc == 1)output = new Test::TextOutput(Test::TextOutput::Verbose);else{const char* arg = argv[1];if (strcmp(arg, "--compiler") == 0)output = new Test::CompilerOutput;else if (strcmp(arg, "--html") == 0)output =  new Test::HtmlOutput;else if (strcmp(arg, "--text-terse") == 0)output = new Test::TextOutput(Test::TextOutput::Terse);else if (strcmp(arg, "--text-verbose") == 0)output = new Test::TextOutput(Test::TextOutput::Verbose);else{cout << "invalid commandline argument: " << arg << endl;usage(); // will not return}}return unique_ptr<Test::Output>(output);
}

函数说明:

  • usage 输出命令参数用法
  • cmdline 根据命令参数构造不同类型输出格式。

3 运行

3.1 Compiler输出

$ ./mytest --compiler
mytest.cpp:62: "unconditional fail"
mytest.cpp:89: 0 == 1
mytest.cpp:100: delta(0.5, 0.7, 0.1)
mytest.cpp:122: func() does not throw, expected int exception
mytest.cpp:123: func_no_throw() does not throw, expected int exception
mytest.cpp:124: func() does not throw, expected any exception
mytest.cpp:125: func_no_throw() does not throw, expected any exception
mytest.cpp:139: func_throw_int() throws an int, expected a float exception
mytest.cpp:140: func_throw_int() throws an int, expected no exception at all

3.2 TextTerse输出

$ ./mytest --text-terse
FailTestSuite: 2/2, 50% correct in 0.000005 seconds
CompareTestSuite: 3/3, 33% correct in 0.000005 seconds
ThrowTestSuite: 2/2, 50% correct in 0.000093 seconds
Total: 7 tests, 42% correct in 0.000103 seconds

3.3 TextVerbose输出

$ ./mytest --text-verbose
FailTestSuite: 2/2, 50% correct in 0.000004 secondsTest:    always_failSuite:   FailTestSuiteFile:    mytest.cppLine:    62Message: "unconditional fail"CompareTestSuite: 3/3, 33% correct in 0.000005 secondsTest:    compareSuite:   CompareTestSuiteFile:    mytest.cppLine:    89Message: 0 == 1Test:    delta_compareSuite:   CompareTestSuiteFile:    mytest.cppLine:    100Message: delta(0.5, 0.7, 0.1)ThrowTestSuite: 2/2, 50% correct in 0.000092 secondsTest:    test_throwSuite:   ThrowTestSuiteFile:    mytest.cppLine:    122Message: func() does not throw, expected int exceptionTest:    test_throwSuite:   ThrowTestSuiteFile:    mytest.cppLine:    123Message: func_no_throw() does not throw, expected int exceptionTest:    test_throwSuite:   ThrowTestSuiteFile:    mytest.cppLine:    124Message: func() does not throw, expected any exceptionTest:    test_throwSuite:   ThrowTestSuiteFile:    mytest.cppLine:    125Message: func_no_throw() does not throw, expected any exceptionTest:    test_throwSuite:   ThrowTestSuiteFile:    mytest.cppLine:    139Message: func_throw_int() throws an int, expected a float exceptionTest:    test_throwSuite:   ThrowTestSuiteFile:    mytest.cppLine:    140Message: func_throw_int() throws an int, expected no exception at allTotal: 7 tests, 42% correct in 0.000101 seconds

3.4 Html格式

html格式输出截图如下:
html格式输出截图


http://www.mrgr.cn/p/16364352

相关文章

git 基础知识(全能版)

文章目录 一 、git 有三个分区二、git 基本操作1、克隆—git clone2、拉取—git fetch / git pull3、查看—git status / git diff3.1 多人开发代码暂存技巧 本地代码4、提交—git add / git commit / git push5、日志—git log / git reflog6、删除—git rm ‘name’7、撤销恢…

vue-解决background-image:url不显示问题

如上图所示,需求是给网页设置背景图,但实际效果是图片无法显示,已经确认地址是没问题的,网上教程有些是让在路径作为参数包裹在require方法里面,但还是未起作用。 折腾许久之后,发现了解决办法,只需要给div设置高度即可<style> .background {height: 120vh; } <…

数据结构与算法解题-20240426

这里写目录标题 面试题 08.04. 幂集367. 有效的完全平方数192. 统计词频747. 至少是其他数字两倍的最大数718. 最长重复子数组 面试题 08.04. 幂集 中等 幂集。编写一种方法&#xff0c;返回某集合的所有子集。集合中不包含重复的元素。 说明&#xff1a;解集不能包含重复的子…

CSS基础语法

CSS 标签选择器 内嵌式改变标签样式 <!DOCTYPE html> <html><head><meta charset"utf-8" /><title></title><!-- 属于标签选择器 --><style>p{font - size: 16px;color: red;}</style></head><bo…

Angular创建项目

Angular创建项目 文章目录 Angular创建项目1. 创建项目1.1 直接安装1.2 跳过npm i安装 2. 运行程序 1. 创建项目 ng new 项目名称 1.1 直接安装 ng new angulardemo --同时会安装依赖包&#xff0c;执行的命令就是npm i 1.2 跳过npm i安装 ng new angulardemo --skip-inst…

dotnet 8 版本与银河麒麟V10和UOS系统的 glibc 兼容性

刚刚好 dotnet 8 的 glibc 版本足够旧,可以运行本文记录于 2024.04.26 如果你阅读本文时间距离本文记录时间过远,可能本文记录的信息已失效 dotnet 根据 dotnet 的 supported-os 文档记录,当前的 dotnet 8 是 8.0.4 版本,官方说明是支持 Debian 11 及以上版本 实际测试可以…

从零入门区块链和比特币(第一期)

欢迎来到我的区块链与比特币入门指南&#xff01;如果你对区块链和比特币感兴趣&#xff0c;但不知道从何开始&#xff0c;那么你来对地方了。本博客将为你提供一个简明扼要的介绍&#xff0c;帮助你了解这个领域的基础知识&#xff0c;并引导你进一步探索这个激动人心的领域。…

U盘格式转换GPT格式转回DOS

当前格式 fdisk /dev/sdb# 在 fdisk 提示符下&#xff0c;输入以下命令删除分区&#xff1a; d # 选择要删除的分区编号&#xff08;如 1、2 等&#xff09; w开始转换 [rootnode-24 ~]# fdisk /dev/sdbWelcome to fdisk (util-linux 2.37.4). Changes will remain in memory o…

RabbitMQ发布确认和消息回退(6)

概念 发布确认原理 生产者将信道设置成 confirm 模式&#xff0c;一旦信道进入 confirm 模式&#xff0c;所有在该信道上面发布的消息都将会被指派一个唯一的 ID(从 1 开始)&#xff0c;一旦消息被投递到所有匹配的队列之后&#xff0c;broker就会发送一个确认给生产者(包含消…

第一个大型汽车ITU-T车载语音通话质量实验室投入使用

中国汽车行业蓬勃发展&#xff0c;尤其是新能源汽车风起云涌&#xff0c;无论是国内还是海外需求旺盛的趋势下&#xff0c;除乘用车等紧凑型车外&#xff0c;中型汽车如MPV、小巴、小型物流车&#xff0c;大型汽车如重卡、泥头车等亦加入了手机互联、智驾的科技行列&#xff0c…

LT9611UXC双端口 MIPI DSI/CSI 转 HDMI2.0,带音频

1. 说明 LT9611UXC 是一款高性能 MIPI DSI/CSI 至 HDMI2.0 转换器。MIPI DSI/CSI 输入具有可配置的单端口或双端口&#xff0c;具有 1 个高速时钟通道和 1~4 个高速数据通道&#xff0c;工作速率最高为 2Gbps/通道&#xff0c;可支持高达 16Gbps 的总带宽。 LT9611UXC 支持突发…

四:物联网ARM开发

一&#xff1a;ARM体系结构概述 1&#xff1a;控制外设led灯还有一些按键这些就要用到gpio&#xff0c;采集传感器的数据需要adc进行转化数据格式&#xff0c;特殊的外设和传感器是通过特殊的协议接口去进行连接的比如一些轴传感器和主控器的连接是通过spi&#xff0c;IIC 控制…

深入解析YOLOv2

深入解析YOLOv2 引言 目标检测是计算机视觉中的一个核心问题&#xff0c;它旨在识别图像中所有感兴趣的目标&#xff0c;并给出它们的类别和位置。近年来&#xff0c;随着深度学习技术的发展&#xff0c;目标检测领域取得了巨大的进步。YOLO&#xff08;You Only Look Once&a…

maven-idea新建和导入项目

全局配置 新建项目 需要新建的文件夹 src/testsrc/test/javasrc/main/java 注&#xff1a;1、新建Java-class&#xff0c;输入.com.hello.hellomaven 2、快捷键psvm显示 public static void main(String[] args) {.... } package com.hello;public class hellomaven {publ…

Pytorch 的实际应用 学习笔记

一. 模型的下载 weights为false时则为没有提前经过训练的模型&#xff0c;为true时则经过了提前训练 vgg16_false torchvision.models.vgg16(weightsFalse) vgg16_true torchvision.models.vgg16(weightsTrue) 打印 二. 模型的修改 &#xff08;1&#xff09;添加操作 …

【机器学习】集成学习:强化机器学习模型与创新能的利器

集成学习&#xff1a;强化机器学习模型预测性能的利器 一、集成学习的核心思想二、常用集成学习方法Bagging方法Boosting方法Stacking方法 三、集成学习代表模型与实现四、总结与展望 在大数据时代的浪潮下&#xff0c;机器学习模型的应用越来越广泛&#xff0c;而集成学习作为…

AJAX——黑马头条-数据管理平台项目

1.项目介绍 功能&#xff1a; 登录和权限判断查看文章内容列表&#xff08;筛选&#xff0c;分页&#xff09;编辑文章&#xff08;数据回显&#xff09;删除文章发布文章&#xff08;图片上传&#xff0c;富文本编辑器&#xff09; 2.项目准备 技术&#xff1a; 基于Bootst…

读天才与算法:人脑与AI的数学思维笔记11_算法如何思考

读天才与算法:人脑与AI的数学思维笔记11_算法如何思考1. 创造力 1.1. 创建一种算法,其首要任务是放弃已知的所有艺术风格,然后判断由算法自己所产生的艺术品是否具有与所有艺术风格都截然不同的特性,即真正独树一帜的艺术风格 1.2. 抗性模型同样适用于人类创造力代码的引导…

考研数学|张宇《1000题》正常用多久刷完?

考研数学1000题的刷题时间因人而异&#xff0c;主要取决于以下几个因素。 首先是个人基础&#xff0c;如果你的数学基础较好&#xff0c;对考研数学的知识点已经比较熟悉&#xff0c;刷题速度可能会更快。 其次是每天投入时间&#xff1a;你每天能够投入多少时间来刷题也会影…

Hadoop伪分布式平台搭建

搭建Hadoop伪分布式环境是在单台机器上模拟完整的Hadoop分布式系统&#xff0c;使得所有的Hadoop守护进程&#xff08;如NameNode、DataNode、ResourceManager、NodeManager等&#xff09;都在同一台机器上运行。这样可以在一台机器上体验Hadoop的分布式特性&#xff0c;适合学…