Bubble's blog
Home
About
catch1.x 测试日志的输出
Category:
技术
Date:
2024-12-12T12:25:56Z
在 Catch 框架中,测试日志的输出明细可以通过以下方式来定制和查看,包括更详细的断言信息、附加日志输出等。以下是一些具体方法: --- ## 1. **使用 Catch 的 `INFO` 和 `CAPTURE` 输出附加信息** Catch 提供了 `INFO` 和 `CAPTURE` 宏,可以在测试中插入额外的信息到日志中,方便调试和排查问题。 ### 示例代码: ```cpp #include <catch.hpp> #include <string> TEST_CASE("Detailed logging example", "[log]") { std::string name = "Program Manager"; std::string title = "Program Manager"; INFO("Checking if name and title are equal"); // 添加额外的日志信息 CAPTURE(name, title); // 捕获变量的值并输出到日志 REQUIRE(name == title); // 断言测试 } ``` ### 输出: ``` Checking if name and title are equal name := "Program Manager" title := "Program Manager" ``` - `INFO` 输出静态日志信息。 - `CAPTURE` 捕获变量名及其当前值。 --- ## 2. **使用自定义消息** `REQUIRE` 和 `CHECK` 支持附加错误消息,便于在断言失败时输出更清晰的上下文。 ### 示例代码: ```cpp #include <catch.hpp> #include <string> TEST_CASE("Custom error messages", "[log]") { std::string expected = "Program Manager"; std::string actual = "Project Manager"; REQUIRE(expected == actual); // 如果失败,输出默认错误信息 REQUIRE(expected == actual && "The strings do not match!"); // 添加自定义错误信息 } ``` ### 输出(断言失败时): ``` failure: The strings do not match! with expansion: "Program Manager" == "Project Manager" ``` --- ## 3. **控制日志级别(捕获更多断言细节)** Catch 支持通过命令行参数来设置日志输出的详细程度,常见的选项包括: - `-s`:显示标准输出内容(默认情况下 Catch 会屏蔽 `std::cout` 的输出)。 - `-r`:设置报告器类型,如 `compact`、`junit`、`xml`。 - `-d yes`:显示断言表达式的扩展。 ### 示例运行命令: ```bash ./tests -s -d yes ``` ### 输出示例: ``` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tests is a Catch2 test binary Run with -? for options ------------------------------------------------------------------------------- Custom error messages ------------------------------------------------------------------------------- example.cpp:11 ............................................................................... example.cpp:14: FAILED: REQUIRE( expected == actual && "The strings do not match!" ) with expansion: "Program Manager" == "Project Manager" =============================================================================== test cases: 1 | 1 failed assertions: 2 | 1 passed | 1 failed ``` --- ## 4. **重定向日志到文件** Catch 提供选项将测试日志输出到文件,便于分析大量测试的日志。 ### 命令行示例: ```bash ./tests --out test_log.txt ``` 所有的测试输出都会被写入 `test_log.txt` 文件。 --- ## 5. **直接使用标准输出** 虽然 Catch 默认会屏蔽 `std::cout` 的输出,但你可以通过 `-s` 选项启用它。在测试中直接输出日志到标准输出(如 `std::cout` 或 `printf`): ### 示例代码: ```cpp #include <catch.hpp> #include <iostream> TEST_CASE("Standard output example", "[log]") { std::string name = "Program Manager"; std::string title = "Project Manager"; std::cout << "Comparing strings: name = " << name << ", title = " << title << std::endl; REQUIRE(name == title); } ``` ### 启用输出的运行命令: ```bash ./tests -s ``` ### 输出: ``` Comparing strings: name = Program Manager, title = Project Manager example.cpp:12: FAILED: REQUIRE( name == title ) with expansion: "Program Manager" == "Project Manager" ``` --- ## 6. **使用自定义报告器** Catch 支持自定义报告器(`Reporter`),你可以通过实现自己的报告器来定制日志输出的格式。 ### 示例代码: ```cpp #include <catch.hpp> struct CustomReporter : Catch::ConsoleReporter { using ConsoleReporter::ConsoleReporter; void assertionEnded(Catch::AssertionStats const& assertionStats) override { ConsoleReporter::assertionEnded(assertionStats); // 输出自定义信息 if (!assertionStats.assertionResult.isOk()) { std::cout << "Custom Log: Assertion failed!" << std::endl; } } }; // 注册自定义报告器 CATCH_REGISTER_REPORTER("custom", CustomReporter) ``` ### 使用方法: ```bash ./tests -r custom ``` --- ## 总结 1. **简单调试**: - 使用 `INFO` 和 `CAPTURE` 添加上下文信息。 2. **自定义日志**: - 添加附加的错误消息。 3. **标准输出**: - 启用标准输出(`-s`)。 4. **日志分析**: - 使用文件输出或自定义报告器。 根据需求选择适合的方式来获取和分析日志。
Share
Permalink
×
0 Comments
latest
No comments.
Name
Email
Site
Comment
Human check: 7 + 9 = ?
Links
CSDN
GitHub
WHOIS
吾爱破解
看雪论坛
Categories
Default
0
技术
24
时事
0
Change Theme
Perfect Blue
Black Swan
0 Comments latest
No comments.