这样的设计不仅有助于保持代码的清晰度,同时也方便了测试的组织与执行。例如,一个简单的测试用例可能如下所示: ```cpp #include <catch2/catch_test_macros.hpp> TEST_CASE("加法运算") { REQUIRE(1 + 1 == 2); } ``` 在这个例子中,`REQUIRE`宏用于验证表达式的结果是否符合预期。如果条件不成立,则测...
编写测试用例:在"test"文件夹中,创建一个名为"test.cpp"的文件,用于编写测试用例。在该文件中,你可以使用Catch2提供的宏和断言来编写各种测试情况。以下是一个简单的示例: 代码语言:txt 复制 #include <catch2/catch.hpp> #include "mylibrary.h" TEST_CASE("Test add function", "[add]") { REQUIRE(add...
#defineCATCH_CONFIG_MAIN// 当前宏强制Catch在当前编译单元中创建 main(),这个宏只能出现在一个CPP文件中,因为一个项目只能有一个有效的main函数#include"catch.hpp"unsignedintFactorial(unsignedintnumber ){returnnumber <=1? number : Factorial(number-1)*number; ...
Test.cpp #defineCATCH_CONFIG_MAIN// 当前宏强制Catch在当前编译单元中创建 main(),这个宏只能出现在一个CPP文件中,因为一个项目只能有一个有效的main函数#include<catch.hpp>TEST_CASE("core::io::First","[test]") {inta =1; REQUIRE(a ==1); ...
// tests.cpp #define CATCH_CONFIG_MAIN #include <catch.hpp> int factorial(int n) { if(n <= 0) return 1; return n * factorial(n - 1); } TEST_CASE("阶乘基础测试", "[math][factorial]") { SECTION("正整数值验证") { REQUIRE(factorial(5) == 120); ...
要自定义Catch2的输出格式,你可以使用CATCH_CONFIG_MAIN宏来设置捕获器的配置。下面是一个示例代码片段,展示了如何自定义Catch2的输出格式: #define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
第一行的作用是由 catch 提供一个 main 函数,第二行的作用是包含测试所需要的头文件,假设最后的文件为 catchTest.cpp ,假设相关的文件安装到了 /usr/local/include 下,下面这样编译就可以了: g++-std=c++11-ocatchTest catchTest.cpp -I/usr/local/include/ ...
// tests.cpp #define CATCH_CONFIG_MAIN #include <catch.hpp> int factorial(int n) { if(n <= 0) return 1; return n * factorial(n - 1); } TEST_CASE("阶乘基础测试", "[math][factorial]") { SECTION("正整数值验证") { REQUIRE(factorial(5) == 120); CHECK(factorial(6) == 720)...
g++ test.cpp -o test -I/path/to/catch2/include # 如果你是手动安装的,需要指定包含路径 ./test 如果编译成功并且运行测试时输出类似All tests passed的信息,那么恭喜你,Catch2已经成功安装在你的Ubuntu系统上了! 注意:如果你使用了vcpkg来安装Catch2,你可能需要指定vcpkg的包含路径和库路径。这通常可以通过...
(NOT Catch2_FOUND) Include(FetchContent) # Fetch and build catch2 FetchContent_Declare( Catch2 GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_TAG v3.3.2 ) FetchContent_MakeAvailable(Catch2) endif() add_executable( tests test.cpp ) target_link_libraries( tests Catch2::Catch2With...