CppTest單元測試框架(更新)

目錄

  • 1 背景
  • 2 設計
  • 3 實現
  • 4 使用
    • 4.1 主函數
    • 4.2 使用方法

1 背景

前面文章單元測試之CppTest測試框架中講述利用宏ADD_SUITE將測試用例自動增加到測試框架中。但在使用中發現一個問題,就是通過宏ADD_SUITE增加多個測試Suite時,每次運行時都是所有測試Suite都運行,有的Suite運行比較慢,這對邊寫測試用例邊編譯運行時效率很低。于是就在原來測試框架下作出修改,即默認運行所有測試用例,不過可以通過命令指定測試用例來運行。

2 設計

修改后新的類圖如下:
類圖

修改說明:

  • TestApp 增加成員suites_,
  • addSuite增加參數name,表示測試Suite名字,該函數實現將suite增加到成員suites_中存起來。
  • run接口沒變,實現時從suites_將suite增加到mainSuite_中,如果沒指定測試用例則全部增加,否則只增加指定測試用例。
  • AutoAddSuite的構造函數增加參數用例名稱。
  • 宏ADD_SUITE參數沒變化,實現時將類型作為測試用例名稱來注冊

類定義如下:

#ifndef TESTAPP_H
#define TESTAPP_H
#include <cpptest/cpptest.h>
#include <map>
#include <memory>class TestApp
{typedef std::map<std::string, std::unique_ptr<Test::Suite>> Suites;Test::Suite mainSuite_;Suites suites_;TestApp();
public:static TestApp& Instance();void  addSuite(const char* name, Test::Suite * suite);int run(int argc, char *argv[]);
};#define theTestApp TestApp::Instance()template<typename Suite>
struct AutoAddSuite
{AutoAddSuite(const char* Name) { theTestApp.addSuite(Name, new Suite()); }
};#define ADD_SUITE(Type) AutoAddSuite<Type>  add##Type(#Type);

說明:

  • TestApp類型是單例類,提高增加Suite接口和run接口
  • AutoAddSuite是一個自動添加Suite的模板類型
  • 宏ADD_SUITE定義了AutoAddSuite對象,用于自動添加。

3 實現

#include "testapp.h"#include <iostream>
#include <tuple>
#include <cstring>
#include <cstdio>namespace
{
void usage()
{std::cout << "usage: test [MODE] [Suite]\n"<< "where MODE may be one of:\n"<< "  --compiler\n"<< "  --html\n"<< "  --text-terse (default)\n"<< "  --text-verbose\n";
}std::tuple<std::string, std::unique_ptr<Test::Output>>
cmdline(int argc, char* argv[])
{Test::Output* output = 0;std::string name;if (argc == 1)output = new Test::TextOutput(Test::TextOutput::Verbose);if(argc > 1){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 if(strcmp(arg, "--help") == 0)std::tuple<std::string, std::unique_ptr<Test::Output>>("help", output);elsestd::cout << "invalid commandline argument: " << arg << std::endl;}if(argc > 2)name = argv[2];return std::tuple<std::string, std::unique_ptr<Test::Output>>(name, output);
}
}TestApp & TestApp::Instance()
{static TestApp theApp;return theApp;
}TestApp::TestApp()
{}void TestApp::addSuite(const char* name, Test::Suite * suite)
{suites_.emplace(name, std::unique_ptr<Test::Suite>(suite));
}int TestApp::run(int argc, char *argv[])
{try{auto params = cmdline(argc, argv);std::string name(std::move(std::get<0>(params)));std::unique_ptr<Test::Output> output(std::move(std::get<1>(params)));if(name == "help" || !output){usage();std::cout << "where Suite may be one of(default - all):\n";for(auto & suite: suites_)std::cout << "  " << suite.first << "\n";return 0;}for(auto & suite: suites_){if(name.empty())mainSuite_.add(std::move(suite.second));else if(name == suite.first){mainSuite_.add(std::move(suite.second));break;}}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到suites_
  • run
    • 首先根據命令行返回Test::Output和要單獨運行測試用例名稱
    • 如果參數錯誤或help顯示用法后退出主程序。
    • 遍歷suites_,將suite添加到mainSuite_中(如果name不為空,則只添加名稱為name的測試用例)
    • 然后調用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 使用方法

這里假定程序名稱concurrent,顯示用法:

 $ ./concurrent --help
usage: test [MODE] [Suite]
where MODE may be one of:--compiler--html--text-terse (default)--text-verbose
where Suite may be one of(default - all):AtomicSuiteBlockQueueSuiteConditionVariableSuiteFutureSuiteLocksSuiteMutexSuiteRingQueueSuiteThreadSuiteTimedMutexSuite

運行測試用例BlockQueueSuite:

$ ./concurrent --text-terse BlockQueueSuite
BlockQueueSuite: 0/2
I get a Apple pie
I get a Banana pie
I get a Pear pie
I get a Plum pie
I get a Pineapple pieI get a Apple pie
I get a Banana pie
I get a Pear pie
I get a Plum pie
I get a Pineapple pieI get a Apple
I get a Banana
I get a Pear
I get a Plum
I get a Pineapple
BlockQueueSuite: 1/2
I get a Apple pie in thread(3)I get a Banana pie in thread(4)I get a Pear pie in thread(0)I get a Plum pie in thread(2)I get a Pineapple pie in thread(1)I get a Apple pie in thread(0)I get a Banana pie in thread(2)I get a Pear pie in thread(3)I get a Plum pie in thread(1)I get a Pineapple pie in thread(4)I get a Apple in thread(1)I get a Banana in thread(0)I get a Pear in thread(2)I get a Plum in thread(3)I get a Pineapple in thread(4)
BlockQueueSuite: 2/2, 100% correct in 0.021808 seconds
Total: 2 tests, 100% correct in 0.021808 seconds

說明:

  • 如上所述只運行測試用例BlockQueueSuite

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/38308.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/38308.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/38308.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

逆向開發環境準備

JDK安裝 AndroidStudio安裝 默認sdk路徑 C:\Users\Administrator\AppData\Local\Android\Sdk 將platform-tools所在的目錄添加到path C:\Users\Administrator\AppData\Local\Android\Sdk\platform-tools 主要目的是使用該目錄下的adb等命令 將tools所在的目錄添加到path C:\Us…

1-5題查詢 - 高頻 SQL 50 題基礎版

目錄 1. 相關知識點2. 例題2.1.可回收且低脂的產品2.2.尋找用戶推薦人2.3.大的國家2.4. 文章瀏覽 I2.5. 無效的推文 1. 相關知識點 sql判斷&#xff0c;不包含null&#xff0c;判斷不出來distinct是通過查詢的結果來去除重復記錄ASC升序計算字符長度 CHAR_LENGTH() 或 LENGTH(…

sqlmap注入詳解

免責聲明:本文僅做分享... 目錄 1.介紹 2.特點 3.下載 4.幫助文檔 5.常見命令 指定目標 請求 HTTP cookie頭 HTTP User-Agent頭 HTTP協議的證書認證 HTTP(S)代理 HTTP請求延遲 設定超時時間 設定重試超時 設定隨機改變的參數值 利用正則過濾目標網址 避免過多的…

Python-爬蟲案例

Python-爬蟲案例 代碼代碼 代碼 import requests import json import threading from queue import Queue import timeclass HeiMaTouTiao:def __init__(self):self.headers {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) ""AppleWebKit/53…

前端筆記-day11

文章目錄 01-空間-平移02-視距03-空間旋轉Z軸04-空間旋轉X軸05-空間旋轉Y軸06-立體呈現07-案例-3D導航08-空間縮放10-動畫實現步驟11-animation復合屬性12-animation拆分寫法13-案例-走馬燈14-案例-精靈動畫15-多組動畫16-全民出游全民出游.htmlindex.css 01-空間-平移 <!D…

基于Spring Boot的在線醫療咨詢平臺的設計與實現【附源碼】

基于Spring Boot的在線醫療咨詢平臺的設計與實現 Design and implementation of the computer hardware mall based on Spring Boot Candidate&#xff1a; Supervisor&#xff1a; April 20th, 2024 學位論文原創性聲明 本人鄭重聲明&#xff1a;所呈交的論文是本人在導師…

初中英語優秀作文分析-006How to Deal with the Exam Stress-如何應對考試壓力

更多資源請關注紐扣編程微信公眾號 記憶樹 1 We students are very busy with schoolwork and in the face of many exams every school day. 翻譯 我們學生忙于功課&#xff0c;每個上學日都面臨許多考試。 簡化記憶 考試 句子結構 We students 主語 我們學生&#xf…

Vite: 高階特性 Pure ESM

概述 ESM 已經逐步得到各大瀏覽器廠商以及 Node.js 的原生支持&#xff0c;正在成為主流前端模塊化方案。 而 Vite 本身就是借助瀏覽器原生的 ESM 解析能力( type“module” )實現了開發階段的 no-bundle &#xff0c;即不用打包也可以構建 Web 應用。不過我們對于原生 ESM 的…

綜合評價類模型——突變級數法

含義 首先&#xff1a;對評價目標進行多層次矛盾分解其次&#xff1a;利用突變理論和模糊數學相結合產生突變模糊隸屬函數再次&#xff1a;由歸一公式進行綜合量化運算最終&#xff1a;歸一為一個參數&#xff0c;即求出總的隸屬函數&#xff0c;從而對評價目標進行排序分析特點…

【linux/shell實戰案例】shell中變量的使用

目錄 一.linux變量聲明及定義 二.linux變量使用方法 三.linux變量使用花括號${name}和雙引號“$name”的區別 四.linux變量使用單引號$name和雙引號“$name”的區別 五.linux變量中使用命令 一.linux變量聲明及定義 #!/bin/bash namezhaodabao 等號兩邊不能有空格變量名…

ES6面試題——箭頭函數和普通函數有什么區別

1. this指向問題 <script> let obj {a: function () {console.log(this); // 打印出&#xff1a;{a: ?, b: ?}},b: () > {console.log(this); // 打印出Window {window: Window, self: Window,...}}, }; obj.a(); obj.b(); </script> 箭頭函數中的this是在箭…

成都市水資源公報(2000-2022年)

數據年限&#xff1a;2000-2022年&#xff0c;無2009年 數據格式&#xff1a;pdf、word、jpg 數據內容&#xff1a;降水量、地表水資源量、地下水資源量、水資源總量、蓄水狀況、平原區淺層地下水動態、水資源情況分析、供水量、用水量、污水處理、洪澇干旱等

類似李跳跳的軟件有什么,強烈推薦所有安卓手機安裝!!!

今天阿星分享一款讓安卓手機更順滑的神器——智慧島。你問我李跳跳&#xff1f;由于大家都知道的原因&#xff0c;那是個曾經讓廣告無處遁形的神兵利器&#xff0c;可惜現在它已經退休了。不過別擔心&#xff0c;智慧島接過了接力棒&#xff0c;繼續為我們的安卓體驗保駕護航。…

Raccon:更好防側信道攻擊的后量子簽名方案

1. 引言 安全社區已經開發出了一些出色的加密算法&#xff0c;這些算法非常安全&#xff0c;但最終&#xff0c;所有的數據都會被存儲在硅和金屬中&#xff0c;而入侵者越來越多地會在那里放置監視器來破解密鑰。 破解加密密鑰通常涉及暴力破解方法或利用實施過程中的缺陷。然…

2029年AI服務器出貨量將突破450萬臺,AI推理服務器即將爆發式增長

在2020年&#xff0c;新冠疫情與遠程辦公模式的興起推動了所有類型服務器的出貨量達到峰值&#xff0c;隨后幾年里&#xff0c;除了AI服務器之外的所有類別都回歸到了正常水平。 根據Omdia的研究數據&#xff0c;AI服務器的出貨量在2020年急劇上升&#xff0c;并且至今未顯示出…

瀏覽器中如何獲取用戶網絡狀態

網頁開發中存在需要獲取用戶是否在線的場景及用戶網絡狀態&#xff0c;瀏覽器提了navigator.onLine和navigator.connection可以實現這一需求。 獲取在線狀態 if (navigator.onLine) {console.log("online"); } else {console.log("offline"); }監聽網絡狀…

日志的介紹

知識鋪墊&#xff1a;在我們日常開發中&#xff0c;其實日志是和我們息息相關的。但可能平常都沒怎么注意到日志相關的知識點&#xff0c;也不怎么關注日志&#xff0c;然后&#xff0c;在生產環境中&#xff0c;日志是必不可少的存在&#xff0c;項目出現問題了都是通過日志來…

cesium 添加 Echarts 圖層(空氣質量點圖)

cesium 添加 Echarts 圖層(下面附有源碼) 1、實現思路 1、在scene上面新增一個canvas畫布 2、通坐標轉換,將經緯度坐標轉為屏幕坐標來實現 3、將ecarts 中每個series數組中元素都加 coordinateSystem: ‘cesiumEcharts’ 2、示例代碼 <!DOCTYPE html> <html lan…

Excel 數據篩選難題解決

人不走空 &#x1f308;個人主頁&#xff1a;人不走空 &#x1f496;系列專欄&#xff1a;算法專題 ?詩詞歌賦&#xff1a;斯是陋室&#xff0c;惟吾德馨 目錄 &#x1f308;個人主頁&#xff1a;人不走空 &#x1f496;系列專欄&#xff1a;算法專題 ?詩詞歌…

緩存穿透、雪崩與擊穿

緩存穿透、雪崩、擊穿 1、緩存穿透強調都沒有數據并發訪問布隆過濾器緩存NULL值 2、緩存雪崩強調批量Key過期并發訪問 3、緩存擊穿強調單個Key過期并發訪問互斥鎖邏輯過期 分布式并發控制 1、緩存穿透 緩存穿透是指數據庫和緩存都沒有的數據&#xff0c;這樣緩存永遠不會生效&…