Catch2的安装


一、工作环境

  windows10下

  Qt编译器

二、Catch2的安装

  闲话部分:由于工作的需要,我不得不开始我的Catch2的开发历程。众所周知,开发中最艰难的就是环境的安装,可以说环境的安装是最让编程人员感到自闭的一个环节。尤其是当你看不懂错误信息,然后上网还查不到相应的错误解决方案的时候,特别让人抓狂。好了,闲话就说到这,开始我们的Catch2安装吧。

    问题高光时刻:①# CATCH_CONFIG_MAIN 无法使用,不能提供main函数(因为你有可能下载的是catch2 V3版本,这个版本不支持这个定义了。)

           ②在Qt编译环境下运行,显示很多的函数未定义。(你在#include<catch.hpp>之前添加c++的编译环境#include<iostream>,然后运行案例就行了。但它是没有输出的,因为它定义的输出是在命令窗口中的。)

   安装步骤:

    1、简单了解catch2,它是一个c++的单元测试工具,它的安装仅仅只需要一个catch.hpp的头文件即可。因为它所需的函数库C++的编译环境都给它备全了。

      2、获取catch.hpp这个头文件(文件获取),仅需获取catch.hpp即可。其他不需要。

     

 

 

 

 

    3、测试是否安装成功(Qt环境下运行),我建立的是cmake项目。先看看我的文件目录。

      

 

 

       将main.cpp文件的内容覆盖,这里我给出我的main.cpp的测试代码。(直接运行项目即可,但是没有输出。)

#include "mainwindow.h"
#include<iostream>
#define CATCH_CONFIG_MAIN
#include<catch.hpp>
#include <QApplication>
using namespace std;


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 );
}

    4、给出在命令行中的测试代码(将这个文件命名为010-TestCase.cpp)

#define CATCH_CONFIG_MAIN
#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 );
}

    命令行中执行这个测试案例(如果你的电脑里不可以使用g++,可以按照教程进行安装)

g++ -std=c++11 -Wall -I$(CATCH_SINGLE_INCLUDE) -o 010-TestCase 010-TestCase.cpp && 010-TestCase --success

     这样你会看到这样的运行结果

 

 

 

      

    


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM