c++ 單元測試框架Catch初探-- Get Started

Catch簡介

  1. Catch 是一個c++ 單元測試框架
  2. Catch是一個單頭文件的測試框架,使用簡單,幾乎不需要配置或安裝其它依賴

Get Started

下載Catch頭文件

  • github.com/catchorg/Catch2 下載catch2.hpp文件
  • 將catch2.hpp文件放入到本地項目

創(chuàng)建測試項目,目錄結構如下

.
├── build
├── CMakeLists.txt
├── main.cpp
└── tests
    ├── catch.hpp
    └── test.cpp

寫測試demo

打開test.cpp

#define CATCH_CONFIG_MAIN  // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"

unsigned int Factorial( unsigned int number ) {
    return number <= 1 ? number : Factorial(number-1)*number;
}

TEST_CASE( "Factorials are computed", "[factorial]" ) {
    REQUIRE( Factorial(1) == 1 );
    REQUIRE( Factorial(2) == 2 );
    REQUIRE( Factorial(3) == 6 );
    REQUIRE( Factorial(10) == 3628800 );
}

編譯

1.修改CMakeLists.txt:

cmake_minimum_required(VERSION 3.14)
project(catchDemo)

set(CMAKE_CXX_STANDARD 14)

add_executable(catchDemo tests/test.cpp)

2.編譯

cd build
cmake ../  
make

3.獲得可執(zhí)行文件


image.png

運行可執(zhí)行文件

  • ./catchDemo
  • 得到運行結果:


    image.png

參考文檔:

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容