美文网首页
c++ 单元测试框架Catch初探-- Get Started

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

作者: HarveyYang777 | 来源:发表于2020-04-20 16:32 被阅读0次

    Catch简介

    1. Catch 是一个c++ 单元测试框架
    2. 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

    参考文档:

    相关文章

      网友评论

          本文标题:c++ 单元测试框架Catch初探-- Get Started

          本文链接:https://www.haomeiwen.com/subject/ejvpvhtx.html