Catch2单元测试框架

一、Catch简介

Catch是一个很时尚的,C++原生的框架,只包含一个头文件,用于单元测试,TDD测试驱动开发和BDD行为驱动开发。
在catch的文档指出,对于C++单元测试框架,目前已经有 Google Test, Boost.Test, CppUnit, Cute, 以及其它的一些,相比较之下catch简单易用、不依赖外部库、支持BDD、测试命名自由等优势。
Catch是一个header-only的开源库,这意味着你只需要把一个头文件放到系统或者你的工程的某个目录,编译的时候指向它就可以了。

二、Catch使用

头文件

github地址:https://github.com/philsquared/Catch

$ git clone https://github.com/philsquared/Catch.git

可以在github直接下载catch.hpp文件,引入到自己的c++工程中。

#include <catch2/catch.hpp>
使用
TEST_CASE() {
    REQUIRE(2 == 2);
}

当然也可以为TEST_CASE起名字,或者加标签:

// test case的名字,全局必须唯一
// "tag1"是标签名,需要放在[]内部。一个test case可以有多个标签,多个test case可以使用相同的标签。
TEST_CASE("Test_name", "[tag1]") {
    REQUIRE(2 == 2); //REQUIRE是一个assert宏,用来判断是否相等。
}

官网用例:

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

三、基本断言

REQUIRE(expression)
CHECK(expression)
REQUIRE_FALSE(expression)
CHECK_FALSE(expression)
注意:REQUIRE和CHECK最主要的区别在于REQUIRE表达式为false时中断执行,而CHECK继续执行。

Matcher比较器

REQUIRE_THAT(lhs, matcher expression)
CHECK_THAT(lhs, matcher expression)
主要内置Matchers
– string matchers:StartsWith, EndsWith, Contains, Equals and Matches
– vector matchers:Contains, VectorContains and Equals
– floating point matchers:WithinULP and WithinAbs
REQUIRE_THAT( str, EndsWith( "as a service", Catch::CaseSensitive::No ) );

浮点数比较

//浮点数比较:
//    epsilon:default std::numeric_limits::epsilon()*100.
//    margin:default 0.0.
//    scale:default 0.0.
TEST_CASE("approx epsilon", "[single-file]")
{
    // 闭区间
    // [100-100*epsilon,100+100*epsilon]
    Approx target = Approx(100).epsilon(0.01);
​
    CHECK(100.0 == target); // Obviously true
    CHECK(99.0 == target); // Obviously true
//    CHECK_FALSE(98.1 == target); // Obviously true
    CHECK_FALSE(98.1 == target); // Obviously true
    CHECK(101.0 == target); // Obviously true
//    CHECK_FALSE(101.1 == target); // Obviously true
    CHECK_FALSE(101.1 == target); // Obviously true
}
TEST_CASE("approx margin", "[single-file]")
{
    // 闭区间
    // [100-margin,100+margin]
    Approx target = Approx(100).margin(1);
​
    CHECK(100.0 == target); // Obviously true
    CHECK(99.0 == target); // Obviously true
    CHECK_FALSE(98.1 == target); // Obviously true
    CHECK(101.0 == target); // Obviously true
    CHECK_FALSE(101.1 == target); // Obviously true
}
信息输出:Logging
    INFO( message expression )
    WARN( message expression )
    FAIL( message expression )
    FAIL_CHECK( message expression )
    CAPTURE( expression1, expression2, … )

四、命令行参数 - TAG

Catch 提供的这个 main 函数实现的另一个强大的功能是丰富的命令行参数,你可以选择执行其中的某些 TEST_CASE,也可以选择不执行其中的某些 TEST_CASE,你可以用它调整输出到 xml 文件,也可以用它从文件中读取需要测试的用例。

需要注意的是,这些强大的命令行大多

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值