Catch簡介
- Catch 是一個c++ 單元測試框架
- Catch是一個單頭文件的測試框架,使用簡單吴藻,幾乎不需要配置或安裝其它依賴
Get Started
下載Catch頭文件
- github.com/catchorg/Catch2 下載catch2.hpp文件
- 將catch2.hpp文件放入到本地項目
創(chuàng)建測試項目惧所,目錄結(jié)構(gòu)如下
.
├── 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í)行文件
運行可執(zhí)行文件
- ./catchDemo
-
得到運行結(jié)果: