C++单元测试工具Catch

来源:互联网 发布:docker overlay网络 编辑:程序博客网 时间:2024/06/08 17:51

Catch是一个很时尚的,C++原生的框架,只包含一个头文件,用于单元测试,TDD测试驱动开发和BDD行为驱动开发。
github地址:https://github.com/philsquared/Catch
git clone https://github.com/philsquared/Catch.git

实验使用环境:
win10企业版,vs2015update3,TortoiseGit2.3.0.0,cmake3.7.2
可以在github地址:https://github.com/philsquared/Catch
直接下载catch.hpp文件,引入到自己的c++工程中使用。
这里写图片描述

注意上面的具体链接会随时更新,请直接到github的catch下载。

从源码获得catch.hpp文件:

01 下载源码

git clone https://github.com/philsquared/Catch.git
这里写图片描述

02 用cmake 生成 vs2015工程

这里写图片描述

默认vs2015 x86版本生成的catch.hpp文件会被安装到

03编译catch,并且生成catch.hpp文件

以管理员权限启动vs2015,打开D:\git\test\Catch\build\x86\CatchSelfTest.sln。并且编译。

不用管理员权限启动vs2015,生成catch.hpp安装到C:\Program Files (x86)\CatchSelfTest\include\catch\catch.hpp会因为权限不足报错。

编译完成后,运行INSTALL工程,生成catch.hpp文件。把这个catch.hpp文件,引入到自己的工程即可。catch测试框架,仅仅这一个catch.hpp文件即可。
这里写图片描述

默认生成目录:
Installing: C:/Program Files (x86)/CatchSelfTest/include/catch/catch.hpp

04 研究catch用法

把SelfTest工程设置为默认启动项,打开SelfTest工程下Tests目录的某个xxxxTests.cpp文件设置断点,或者从TestMain.cpp入手,即可研究catch.hpp的具体使用方法。Tests下面的这些cpp就是具体使用catch.hpp的测试用例。模仿即可。
这里写图片描述

05 官网用例介绍

https://github.com/philsquared/Catch/blob/master/docs/tutorial.md

#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 );}
0 0
原创粉丝点击