Catch简介
- Catch 是一个c++ 单元测试框架
- Catch是一个单头文件的测试框架,使用简单,几乎不需要配置或安装其它依赖
Get Started
下载Catch头文件
- github.com/catchorg/Catch2 下载catch2.hpp文件
- 将catch2.hpp文件放入到本地项目
创建测试项目,目录结构如下
.
├── 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.获得可执行文件
image.png
运行可执行文件
- ./catchDemo
-
得到运行结果:
image.png
网友评论