From 2e18ec3d87c84eecb273f2873120e8cfbb848b63 Mon Sep 17 00:00:00 2001 From: xueben Date: Sun, 8 Feb 2026 22:09:59 +0800 Subject: [PATCH] =?UTF-8?q?ENH:=20=E4=BC=98=E5=8C=96=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E5=99=A8=E8=BE=93=E5=87=BA=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=A4=B1=E8=B4=A5=E7=94=A8=E4=BE=8B=E6=B1=87=E6=80=BB?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Why: 当前测试用例较多(48+),当发生测试失败时,错误信息淹没在大量的控制台输出中,开发者需要向上翻页查找具体的失败位置,影响调试效率。 What: 修改 test/test_main_new.c,实现了失败用例的自动收集与汇总: 1. 定义静态数组 failed_tests(容量 200),用于存储失败测试名称; 2. 利用 Unity 的 tearDown 钩子,在检测到 Unity.CurrentTestFailed 时自动记录当前测试名; 3. 在 main 函数末尾(测试执行结束后)新增汇总打印逻辑,直接列出所有失败的测试用例名称。 TEST: 测试结束后,控制台最末尾将清晰显示失败用例列表,开发者可一眼定位问题。 --- test/test_main_new.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/test/test_main_new.c b/test/test_main_new.c index fe49599..f5cbd7a 100644 --- a/test/test_main_new.c +++ b/test/test_main_new.c @@ -111,6 +111,11 @@ void performance_tests_teardown(void) { // ==================== Unity标准设置函数 ==================== +// 定义最大失败记录数(200个足够容纳所有测试用例) +#define MAX_FAILED_TESTS 200 +static const char* failed_tests[MAX_FAILED_TESTS]; +static int failed_test_count = 0; + void setUp(void) { // Unity会在每个测试前调用 test_framework_reset(); @@ -118,7 +123,11 @@ void setUp(void) { void tearDown(void) { // Unity会在每个测试后调用 - // 清理工作 + if (Unity.CurrentTestFailed) { + if (failed_test_count < MAX_FAILED_TESTS) { + failed_tests[failed_test_count++] = Unity.CurrentTestName; + } + } } // ==================== 主函数 ==================== @@ -210,6 +219,22 @@ int main(void) { // 清理资源 test_framework_cleanup(); - // 返回测试结果 - return UNITY_END(); + // 获取测试结果 + int result = UNITY_END(); + + // 打印失败用例汇总 + if (failed_test_count > 0) { + printf("\n========================================\n"); + printf(" 失败用例汇总\n"); + printf("========================================\n"); + for (int i = 0; i < failed_test_count; i++) { + printf("[FAIL] %s\n", failed_tests[i]); + } + if (failed_test_count == MAX_FAILED_TESTS) { + printf("... (更多失败未显示)\n"); + } + printf("========================================\n"); + } + + return result; } \ No newline at end of file