单元测试之CppTest测试框架

2024-06-05 20:28

本文主要是介绍单元测试之CppTest测试框架,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

  • 1 背景
  • 2 设计
  • 3 实现
  • 4 使用
    • 4.1 主函数
    • 4.2 测试用例
      • 定义
      • 实现
    • 4.3 运行

1 背景

前面文章CppTest实战演示中讲述如何使用CppTest库。其主函数如下:

int main(int argc, char *argv[])
{Test::Suite mainSuite;Test::TextOutput output(Test::TextOutput::Verbose);mainSuite.add(std::unique_ptr<Test::Suite>(new SeesionSuite));mainSuite.run(output, true);return 0;
}

以上代码有一点不好,就是每增加一个测试Suite就需要在main函数中调用mainSuite.add增加用例。有没有办法测试Suite自动添加,不需要修改main函数。下面讲述的测试框架可以解决这个问题。

2 设计

首先设计类TestApp,该类是单例模式,可以添加测试Suite,其次AutoAddSuite是一模板类在其构造函数中自动添加测试Suite.
其类图如下:
类图

类定义如下:

class TestApp
{Test::Suite mainSuite_;TestApp();
public:static TestApp& Instance();void  addSuite(Test::Suite * suite);int run(int argc, char *argv[]);
};
#define theTestApp TestApp::Instance()template<typename Suite>
class AutoAddSuite
{Suite* suite;
public:AutoAddSuite(): suite(new Suite()){ theTestApp.addSuite(suite);}
};
#define ADD_SUITE(Type) AutoAddSuite<Type>  add##Type

说明:

  • TestApp类型是单例类,提高增加Suite接口和run接口
  • AutoAddSuite是一个自动添加Suite的模板类型
  • 宏ADD_SUITE定义了AutoAddSuite对象,用于自动添加。

3 实现

#include "testapp.h"#include <iostream>
#include <cstring>
#include <cstdio>namespace
{
void usage()
{std::cout << "usage: test [MODE]\n"<< "where MODE may be one of:\n"<< "  --compiler\n"<< "  --html\n"<< "  --text-terse (default)\n"<< "  --text-verbose\n";exit(0);
}std::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{std::cout << "invalid commandline argument: " << arg << std::endl;usage(); // will not return}}return std::unique_ptr<Test::Output>(output);
}
}TestApp & TestApp::Instance()
{static TestApp theApp;return theApp;
}TestApp::TestApp()
{}void TestApp::addSuite(Test::Suite * suite)
{mainSuite_.add(std::unique_ptr<Test::Suite>(suite));
}int TestApp::run(int argc, char *argv[])
{try{std::unique_ptr<Test::Output> output(cmdline(argc, argv));mainSuite_.run(*output, true);Test::HtmlOutput* const html = dynamic_cast<Test::HtmlOutput*>(output.get());if (html)html->generate(std::cout, true, argv[0]);}catch (...){std::cout << "unexpected exception encountered\n";return EXIT_FAILURE;}return EXIT_SUCCESS;
}

说明:

  • Instance 返回一个单例引用
  • addSuite 增加Suite到mainSuite_
  • run
    • 首先根据命令行返回Test::Output
    • 然后调用mainSuite_运行测试用例
    • 最后如果类型是Output是Test::HtmlOutput类型,则将结果输出到标准输出std::cout.

4 使用

4.1 主函数

#include "testapp.h"int main(int argc, char *argv[])
{try{theTestApp.run(argc, argv);}catch(const std::exception& e){std::cerr << e.what() << '\n';}return 0;
}

主函数很简单,不再详述。

4.2 测试用例

这里使用C++标准库中std::mutex作为测试示例.

定义

#ifndef MUTEX_TEST_H
#define MUTEX_TEST_H
#include <cpptest/cpptest.h>class MutexSuite : public Test::Suite
{
public:MutexSuite(){TEST_ADD(MutexSuite::construct)TEST_ADD(MutexSuite::lock)TEST_ADD(MutexSuite::try_lock)TEST_ADD(MutexSuite::unlock)}void construct();void lock();void try_lock();void unlock();
};
#endif

说明:

  • cpptest库标准使用,不再详述。

实现

#include "mutex_test.h"
#include "testapp.h"#include <thread>
#include <mutex>ADD_SUITE(MutexSuite);void addCount(std::mutex & mutex, int & count)
{mutex.lock();count++;mutex.unlock();
}void MutexSuite::construct()
{std::mutex muxtex;int count = 0;std::thread threads[10];for(int i = 0; i < 10; i++)threads[i] = std::thread(addCount, std::ref(muxtex), std::ref(count));for(auto &thread : threads)thread.join();TEST_ASSERT_EQUALS(10, count)   
}void MutexSuite::lock()
{std::mutex muxtex;int count = 10;std::thread threads[10];for(int i = 0; i < 10; i++)threads[i] = std::thread(addCount, std::ref(muxtex), std::ref(count));for(auto &thread : threads)thread.join();TEST_ASSERT_EQUALS(20, count)   
}struct Function
{volatile int counter = 0;void add_10k_count(std::mutex & muxtex){for(int i = 0; i < 10000; i++){if(muxtex.try_lock()){++counter;muxtex.unlock();}}}
};void MutexSuite::try_lock()
{std::mutex muxtex;Function function;std::thread threads[10];for(int i = 0; i < 10; i++)threads[i] = std::thread(&Function::add_10k_count, std::ref(function), std::ref(muxtex));for(auto &thread : threads)thread.join();TEST_ASSERT_EQUALS(true, function.counter < (10 * 10000))std::cerr << "function.counter: " << function.counter << std::endl;
}void MutexSuite::unlock()
{std::mutex muxtex;int count = 20;std::thread threads[10];for(int i = 0; i < 10; i++)threads[i] = std::thread(addCount, std::ref(muxtex), std::ref(count));for(auto &thread : threads)thread.join();TEST_ASSERT_EQUALS(30, count)   
}

说明:

  • 重点说明下文件头部宏ADD_SUITE的使用,如下所示传递给ADD_SUITE的参数正是MutexSuite,通过该定义,将MutexSuite自动添加到TestApp实例中,不需要修改main函数.
ADD_SUITE(MutexSuite);

4.3 运行

$ testapp --html

说明:

  • testapp是编译出的可执行文件
  • 参数–html 说明测试报告按网页格式输出.

这篇关于单元测试之CppTest测试框架的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1034093

相关文章

python多线程并发测试过程

《python多线程并发测试过程》:本文主要介绍python多线程并发测试过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、并发与并行?二、同步与异步的概念?三、线程与进程的区别?需求1:多线程执行不同任务需求2:多线程执行相同任务总结一、并发与并行?1、

C++ HTTP框架推荐(特点及优势)

《C++HTTP框架推荐(特点及优势)》:本文主要介绍C++HTTP框架推荐的相关资料,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Crow2. Drogon3. Pistache4. cpp-httplib5. Beast (Boos

SpringBoot基础框架详解

《SpringBoot基础框架详解》SpringBoot开发目的是为了简化Spring应用的创建、运行、调试和部署等,使用SpringBoot可以不用或者只需要很少的Spring配置就可以让企业项目快... 目录SpringBoot基础 – 框架介绍1.SpringBoot介绍1.1 概述1.2 核心功能2

Spring框架中@Lazy延迟加载原理和使用详解

《Spring框架中@Lazy延迟加载原理和使用详解》:本文主要介绍Spring框架中@Lazy延迟加载原理和使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、@Lazy延迟加载原理1.延迟加载原理1.1 @Lazy三种配置方法1.2 @Component

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应

Python GUI框架中的PyQt详解

《PythonGUI框架中的PyQt详解》PyQt是Python语言中最强大且广泛应用的GUI框架之一,基于Qt库的Python绑定实现,本文将深入解析PyQt的核心模块,并通过代码示例展示其应用场... 目录一、PyQt核心模块概览二、核心模块详解与示例1. QtCore - 核心基础模块2. QtWid

最新Spring Security实战教程之Spring Security安全框架指南

《最新SpringSecurity实战教程之SpringSecurity安全框架指南》SpringSecurity是Spring生态系统中的核心组件,提供认证、授权和防护机制,以保护应用免受各种安... 目录前言什么是Spring Security?同类框架对比Spring Security典型应用场景传统

Python结合Flask框架构建一个简易的远程控制系统

《Python结合Flask框架构建一个简易的远程控制系统》这篇文章主要为大家详细介绍了如何使用Python与Flask框架构建一个简易的远程控制系统,能够远程执行操作命令(如关机、重启、锁屏等),还... 目录1.概述2.功能使用系统命令执行实时屏幕监控3. BUG修复过程1. Authorization

SpringBoot集成图片验证码框架easy-captcha的详细过程

《SpringBoot集成图片验证码框架easy-captcha的详细过程》本文介绍了如何将Easy-Captcha框架集成到SpringBoot项目中,实现图片验证码功能,Easy-Captcha是... 目录SpringBoot集成图片验证码框架easy-captcha一、引言二、依赖三、代码1. Ea