Files
smart-home-control-system/tests/test_runner.c
T
sambu 578a770737 chore(M0): 建立 CLion/CMake 工程骨架与测试框架
- CMake 多目标工程:shs_core 静态库 + smart_home 可执行 + shs_tests 测试
- CMakePresets.json:debug/release/asan/coverage 四个预设(debug 告警即错误)
- 自研零依赖测试框架 tests/framework/test_framework.h + 运行器(套件过滤、CTest 集成)
- 工具:tools/header_check.sh 头文件独立编译检查(接口冻结门)
- 已实现公共层:errors / string_utils / num_utils,main 启动自检
- 实测:debug 预设 0 告警、ctest 3/3 通过(45 项断言)
2026-09-14 19:34:18 +08:00

100 lines
2.7 KiB
C

/**
* @file test_runner.c
* @brief 测试入口:按套件名选择运行,并与 CTest 集成
*
* 用法:
* ./shs_tests # 运行全部套件
* ./shs_tests --suite unit.string
* ./shs_tests --list # 列出全部套件
* ./shs_tests --no-color
*/
#include "framework/test_framework.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* 各测试套件的声明(新增套件时在此与 g_suites 表中登记) */
void suite_unit_string(void);
void suite_unit_num(void);
typedef struct {
const char *name; /* 套件名,与 CTest 中注册的名字一致 */
tf_suite_fn fn;
const char *desc; /* 教材知识点说明 */
} test_suite_t;
static const test_suite_t g_suites[] = {
{ "unit.string", suite_unit_string, "项目五 字符数组与字符串处理" },
{ "unit.num", suite_unit_num, "项目二 数据类型、运算符与类型转换" },
};
#define SUITE_COUNT ((int)(sizeof(g_suites) / sizeof(g_suites[0])))
static bool is_tty_stdout(void)
{
#ifdef _WIN32
return false;
#else
return true;
#endif
}
static void run_suite(const test_suite_t *suite)
{
printf("\n=== 套件 %s (%s) ===\n", suite->name, suite->desc);
tf_begin(suite->name);
suite->fn();
(void)tf_end();
}
int main(int argc, char **argv)
{
const char *only = NULL;
bool list_only = false;
bool use_color = is_tty_stdout();
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--suite") == 0 && i + 1 < argc) {
only = argv[++i];
} else if (strcmp(argv[i], "--list") == 0) {
list_only = true;
} else if (strcmp(argv[i], "--no-color") == 0) {
use_color = false;
} else {
fprintf(stderr, "未知参数: %s\n", argv[i]);
return 2;
}
}
tf_set_color(use_color);
if (list_only) {
for (int i = 0; i < SUITE_COUNT; i++) {
printf("%s\t%s\n", g_suites[i].name, g_suites[i].desc);
}
return 0;
}
int matched = 0;
for (int i = 0; i < SUITE_COUNT; i++) {
if (only != NULL && strcmp(only, g_suites[i].name) != 0) {
continue;
}
run_suite(&g_suites[i]);
matched++;
}
if (only != NULL && matched == 0) {
fprintf(stderr, "未找到套件: %s\n", only);
return 2;
}
printf("\n---------------- 测试汇总 ----------------\n");
printf("套件数 : %d\n", tf_suites_run);
printf("断言数 : %d\n", tf_total_checks);
printf("失败数 : %d\n", tf_total_failures);
printf("结果 : %s\n", (tf_total_failures == 0) ? "全部通过" : "存在失败");
printf("------------------------------------------\n");
return (tf_total_failures == 0) ? 0 : 1;
}