VS2017 C++ Catch单元测试-实例 tcy
生活随笔
收集整理的這篇文章主要介紹了
VS2017 C++ Catch单元测试-实例 tcy
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
網上很多資源大都原文檔的翻譯,具體怎么做我沒有找到更好的實例,于是自己寫一個函數及類的基本測試步驟。
本著使用,理論不講。基本的注意事項我在程序中有注釋。我用catch的原因是方便,不用設置VS,代碼容易理解,簡單易上手,對于ATL的調試比較復雜,在此沒有涉及。
原文檔很多講在控制臺下的調試(參考我的上一篇博文)我覺得用途不是太大,且需要一些時間學習。按照本測試方式你能馬上上手,僅僅需要包含頭文件TEST_MAIN.h就可以。
原因嗎你慢慢研究吧!原代碼下載 https://download.csdn.net/download/tcy23456/16660876
?
//TEST_MAIN.h#pragma once /******************************************************** 用途:Test Main()入口函數 時間:2021/4/13 tcy shanghai songjiang xiaokunshan V1.0 ********************************************************/#include <catch2/catch.hpp>//捕獲變量或表達式的值 #define print_variable(v,...) CAPTURE(v, ## __VA_ARGS__ )//自定義測試函數: //void assert_(int x1, int x2); #define assert_(x1, x2) CHECK((x1) == (x2)) //錯誤不退出 #define assert_Require(x1, x2) REQUIRE((x1) == (x2))//錯誤退出//顯示函數: void print(const std::string& str); void print(const char*str); //TEST_MAIN.cpp/********************************************************************** 用途:Test Main()測試程序入口;每個方案只能有一個 時間:2021-4-13 tcy shanghai songjiang xiaokunshan 說明: catch測試僅需要包含頭文件,不需要配置設置,不需要main()直接使用注意:1)下面可寫函數或測試代碼,但不建議;可定義測試類的公共函數2)你應編寫專用的測試代碼文件,僅僅在頭部添加:#include "TEST_MAIN.h"3) 測試程序不能包含int main(){}4) 若定義函數必須在xxx.h聲明(聲明定義錯誤)在xxx.cpp定義**********************************************************************/#define CATCH_CONFIG_MAIN //Test Main()測試程序入口;每個方案只能有一個 #include "TEST_MAIN.h"/********************************************************************** 自定義測試函數: 說明:這樣定義無問題,能正常使用。 缺點:若拋出異常會定位在函數中,不方便定位在調用程序查找錯誤點 改善:用宏定義替代函數定義 **********************************************************************/ //void assert_(int x1, int x2) { CHECK((x1) == (x2)); }//顯示函數: void print(const std::string& str) {/*1)測試正常不會顯示消失(控制臺模式可以)2)要想顯示消息必須在測試程序TEST_CASE()尾部添加CHECK(false);*/UNSCOPED_INFO(str);//INFO("測試開始..."); }void print(const char*str) {UNSCOPED_INFO(std::string(str)); } Func.h#pragma onceint add_(int a, int b); int sub_(int a, int b); int mul_(int a, int b); int div_(int a, int b);int div1(int x, int y); int div2(int x, int y); Func.cpp #include "Func.h"int add_(int a, int b) { return a + b; } int sub_(int a, int b) { return a - b; } int mul_(int a, int b) { return a * b; } int div_(int a, int b) { return a / b; }int div1(int x, int y) { return x / y; } int div2(int x, int y) { if (y == 0) throw "error y=0"; return x / y; } BoyClass.h #pragma once #include <string>//int add(int x,int y); //int sub(int x,int y);class BoyClass { public:void set_name(std::string name) { this->name = name; }void set_age(int age) { this->age = age; }std::string get_name() { return name; }int get_age() { return age; } private:std::string name = "";int age = 0; }; testBase.cpp #include "BoyClass.h" //包含要測試的類函數 #include "Func.h" #include "TEST_MAIN.h"TEST_CASE("測試CHECK REQUIRE") {INFO("1.1.CHECK測試開始...");CHECK(1);INFO("1.2.CHECK(1)完成 Test=ok.");CHECK(0);INFO("1.3.CHECK(0)完成 Test = ng.");CHECK(1);INFO("2.1.REQUIRE測試開始...");REQUIRE(1);INFO("2.2.REQUIRE(1)完成 Test=ok.");REQUIRE(0);INFO("2.3.REQUIRE(0)完成fail.下面測試不執行,不顯示本條消息.進入下一個測試test add1");REQUIRE(1); } /*1.1.CHECK測試開始...1.2.CHECK(1)完成 Test=ok.1.3.CHECK(0)完成 Test = ng.2.1.REQUIRE測試開始...2.2.REQUIRE(1)完成 Test=ok. */TEST_CASE("測試THROW:", "[arg_throw]") {CHECK_NOTHROW((1 / 2));CHECK_THROWS(div2(1, 0));//ok.確實拋出啦異常CHECK_THROWS(div2(1, 1));//ng.程序沒有拋出異常REQUIRE_THROWS_WITH(div2(1, 0), "error y=0"); }TEST_CASE("測試float:", "[arg_float]") {using namespace Catch::literals;//默認最多5位小數點精度CHECK(3.14156 == Approx(3.1415926535898));//trueCHECK(3.14155 == Approx(3.1415926535898));//false//上面的簡寫CHECK(3.14156 == 3.1415926535898_a); //trueCHECK(3.14155 == 3.1415926535898_a); //false//最多+-1%偏差CHECK(101 == Approx(100).epsilon(0.01)); //trueCHECK(99 == Approx(100).epsilon(0.01)); //true//最多+-2偏差CHECK(98 == Approx(100).margin(2)); //trueCHECK(102 == Approx(100).margin(2)); //true }TEST_CASE("測試顯示消息:", "[arg_msg]") {assert_(1, 1);//okassert_(1, 2);//ngprint("start test...");for (int i = 0; i < 3; ++i)UNSCOPED_INFO("數字 i=" << (i + 1));Approx v = Approx(3.1415926535898);auto err_msg=Catch::Message("pi = " + v.toString());print("err_msg=" + err_msg.toString()+'\n');print("err_msg=" + err_msg.describe() + '\n');INFO("準備捕獲變量或表達式的值:");int a = 1, b = 2, c = 3;CAPTURE(a, b, c, a + b, c > b, a == 1);CAPTURE((std::pair<int, int>{1, 2}));//快速捕獲變量或表達式的值CHECK(false);//必須false否則不會顯示消息 }/*start test...數字 i=1數字 i=2數字 i=3err_msg=exception message matches "pi = Approx( 3.1415926536 )"err_msg=exception message matches "pi = Approx( 3.1415926536 )"準備捕獲變量或表達式的值:a := 1b := 2c := 3a + b := 3c > b := truea == 1 := true(std::pair<int, int>{1, 2}) := {?} */?
testFunc.cpp#include "Func.h" //包含要測試的函數 #include "TEST_MAIN.h" //測試MAIN()入口/* 此處可以定義要測試的函數或類:不建議 int add(int a, int b) { return a + b; } */TEST_CASE("測試Func_add sub mul", "[arg_add],[arg_sub],[arg_mul]") {int a = 1, b = 2;SECTION("a = 1, b = 2", "[add()==3]") { REQUIRE(add_(a,b) == 3);}SECTION("a = 1, b = 2", "[sub()==3]") { REQUIRE(sub_(a, b) == -1); }SECTION("a = 1, b = 2", "[mul()==3]") { REQUIRE(mul_(a, b) == 2); }SECTION("a = 1, b = 2", "[add()==3]") { REQUIRE(add_(a, b) == 4); } }TEST_CASE("測試Func_div", "[arg_div]") {int a = 10, b = 2;REQUIRE(b != 0);SECTION("a = 10, b = 2", "[div()==3]") { REQUIRE(div_(a, b) == 5); }SECTION("a = 10, b = 0", "[div()==3]") { REQUIRE(div_(a, b) == 5); }//循環測試:for (int i = -3; i < 3; ++i) {//SECTION(std::string("Looped section: i=") + i)DYNAMIC_SECTION("Looped section: i= " << i) {REQUIRE(i!=0);REQUIRE(div_(a, i) == a/i);}} } testBoyClass.cpp #include "BoyClass.h" //包含要測試的類函數 #include "TEST_MAIN.h" //測試MAIN()入口/* 此處可以定義要測試的函數或類:不建議 int add(int a, int b) { return a + b; } */TEST_CASE("測試BoyClass", "[arg_method]") {INFO("1.測試BoyClass_1測試開始...");BoyClass b;std::string name = "Tom";int age = 22;REQUIRE(!name.empty());REQUIRE(age>0);SECTION("name=Bob", "[get_name()=='Bob']") { name = "Bob"; REQUIRE(!name.empty());b.set_name(name);REQUIRE(b.get_name() == name);}SECTION("age=30", "[get_age()==30]") {age = 30; REQUIRE(age>0);b.set_age(age); REQUIRE(b.get_age() == age);}SECTION("age=40", "[get_age()==40]") {age = 40; REQUIRE(age > 0);b.set_age(age); REQUIRE(b.get_age() == age+1);} } /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tcy_Project.exe is a Catch v2.13.4 host application. Run with -? for options------------------------------------------------------------------------------- 測試CHECK REQUIRE ------------------------------------------------------------------------------- c:\users\administrator\source\repos\tcy_project\tcy_project\test_base.cpp(6) ...............................................................................c:\users\administrator\source\repos\tcy_project\tcy_project\test_base.cpp(10): FAILED:CHECK( 0 ) with messages:1.1.CHECK測試開始...1.2.CHECK(1)完成 Test=ok.c:\users\administrator\source\repos\tcy_project\tcy_project\test_base.cpp(17): FAILED:REQUIRE( 0 ) with messages:1.1.CHECK測試開始...1.2.CHECK(1)完成 Test=ok.1.3.CHECK(0)完成 Test = ng.2.1.REQUIRE測試開始...2.2.REQUIRE(1)完成 Test=ok.------------------------------------------------------------------------------- 測試THROW: ------------------------------------------------------------------------------- c:\users\administrator\source\repos\tcy_project\tcy_project\test_base.cpp(29) ...............................................................................c:\users\administrator\source\repos\tcy_project\tcy_project\test_base.cpp(32): FAILED:CHECK_THROWS( div2(1, 1) ) because no exception was thrown where one was expected:------------------------------------------------------------------------------- 測試float: ------------------------------------------------------------------------------- c:\users\administrator\source\repos\tcy_project\tcy_project\test_base.cpp(36) ...............................................................................c:\users\administrator\source\repos\tcy_project\tcy_project\test_base.cpp(41): FAILED:CHECK( 3.14155 == Approx(3.1415926535898) ) with expansion:3.14155 == Approx( 3.1415926536 )c:\users\administrator\source\repos\tcy_project\tcy_project\test_base.cpp(45): FAILED:CHECK( 3.14155 == 3.1415926535898_a ) with expansion:3.14155 == Approx( 3.1415926536 )------------------------------------------------------------------------------- 測試顯示消息: ------------------------------------------------------------------------------- c:\users\administrator\source\repos\tcy_project\tcy_project\test_base.cpp(57) ...............................................................................c:\users\administrator\source\repos\tcy_project\tcy_project\test_base.cpp(59): FAILED:CHECK( (1) == (2) ) with expansion:1 == 2c:\users\administrator\source\repos\tcy_project\tcy_project\test_base.cpp(78): FAILED:CHECK( false ) with messages:start test...數字 i=1數字 i=2數字 i=3err_msg=exception message matches "pi = Approx( 3.1415926536 )"err_msg=exception message matches "pi = Approx( 3.1415926536 )"準備捕獲變量或表達式的值:a := 1b := 2c := 3a + b := 3c > b := truea == 1 := true(std::pair<int, int>{1, 2}) := {?}------------------------------------------------------------------------------- 測試Func_add sub mula = 1, b = 2 ------------------------------------------------------------------------------- c:\users\administrator\source\repos\tcy_project\tcy_project\test_func.cpp(12) ...............................................................................c:\users\administrator\source\repos\tcy_project\tcy_project\test_func.cpp(12): FAILED:REQUIRE( add_(a, b) == 4 ) with expansion:3 == 4------------------------------------------------------------------------------- 測試Func_divLooped section: i= 0 ------------------------------------------------------------------------------- c:\users\administrator\source\repos\tcy_project\tcy_project\test_func.cpp(29) ...............................................................................c:\users\administrator\source\repos\tcy_project\tcy_project\test_func.cpp(30): FAILED:REQUIRE( i!=0 ) with expansion:0 != 0------------------------------------------------------------------------------- 測試BoyClassage=40 ------------------------------------------------------------------------------- c:\users\administrator\source\repos\tcy_project\tcy_project\test_boyclass.cpp(24) ...............................................................................c:\users\administrator\source\repos\tcy_project\tcy_project\test_boyclass.cpp(26): FAILED:REQUIRE( b.get_age() == age+1 ) with expansion:40 == 41 with message:1.測試BoyClass_1測試開始...=============================================================================== test cases: 7 | 0 passed | 7 failed assertions: 59 | 49 passed | 10 failedC:\Users\Administrator\source\repos\tcy_Project\x64\Debug\tcy_Project.exe (進程 4508)已退出,返回代碼為: 10。 若要在調試停止時自動關閉控制臺,請啟用“工具”->“選項”->“調試”->“調試停止時自動關閉控制臺”。 按任意鍵關閉此窗口...*/?
總結
以上是生活随笔為你收集整理的VS2017 C++ Catch单元测试-实例 tcy的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VS2017目录结构-多项目开发 tc
- 下一篇: C3沙龙北京第十三次活动整理