c++ 单元测试框架Catch初探-- Get Started

2020-04-20  本文已影响0人  HarveyYang777

Catch简介

  1. Catch 是一个c++ 单元测试框架
  2. Catch是一个单头文件的测试框架,使用简单,几乎不需要配置或安装其它依赖

Get Started

下载Catch头文件

创建测试项目,目录结构如下

.
├── 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

运行可执行文件

参考文档:

上一篇 下一篇

猜你喜欢

热点阅读