Catch是一個不錯的單元測試框架,幫助刷Leetcode
github在此
使用也比較簡單,最簡單的方式就是直接下載Catch.hpp,做好引用。
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
#include <vector>
using namespace std;
unsigned int Factorial( unsigned int number ) {
return number > 1 ? Factorial(number-1)*number : 1;
}
TEST_CASE( "Factorials are computed", "[factorial]" ) {
REQUIRE( Factorial(0) == 1);
REQUIRE( Factorial(1) == 1 );
REQUIRE( Factorial(2) == 2 );
REQUIRE( Factorial(3) == 6 );
REQUIRE( Factorial(10) == 3628800 );
}
直接編譯運行就可以知道測試結果了。可以在TEST_CASE裏面聲明類同樣進行測試。
* tips *:同時用g++ 編譯是可以用g++ -std=c++11 test.cpp 使得cpp文件以c++11標準編譯。