c++单元测试框架Catch

来源:互联网 发布:大数据o2o概念股龙头 编辑:程序博客网 时间:2024/06/05 03:59


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标准编译。

2 0
原创粉丝点击