新增 - tests/integration/test_menu.c:菜单 14 项全分派、命令全集正/负例、参数解析、脚本化 stdin 主循环 - tests/integration/test_http_api.c:其余 11 个接口 + 错误路径(400/404/协议层 400) - src/app/app_options.c:app_parse_args/app_print_usage 从 main.c 迁入静态库(app.h 声明此前无实现) - app_clear_stop():复用主循环时清除退出标志 - tools/coverage.sh:A10 覆盖率门限自动判定;acceptance 扩到 A1~A10 共 25 项 修复(本轮集成测试暴露) - menu_render 漏渲染首项「设备列表」(行号 0 既当标题又当表项 0) - http_server_stop 只置标志不关套接字,导致同进程无法二次启动(聚合运行 70 项失败) - POST /api/config 未匹配到字段时假成功(现返回 400) - CRC/I2C 字节序、采样路数、离线设备负例等此前已在集成阶段修复 实测:CTest 19/19;聚合运行 19 套件 2450 断言 0 失败;ASan 19/19;覆盖率 80.3%
134 lines
4.7 KiB
C
134 lines
4.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_func(void);
|
|
void suite_unit_struct(void);
|
|
void suite_unit_protocol(void);
|
|
void suite_unit_hal(void);
|
|
void suite_unit_json(void);
|
|
void suite_integration_http(void);
|
|
void suite_unit_string(void);
|
|
void suite_integration_system(void);
|
|
void suite_integration_fault(void);
|
|
void suite_integration_http_api(void);
|
|
void suite_integration_menu(void);
|
|
void suite_unit_num(void);
|
|
void suite_unit_branch(void);
|
|
void suite_unit_loop(void);
|
|
void suite_unit_bitops(void);
|
|
void suite_unit_errors(void);
|
|
void suite_unit_file(void);
|
|
void suite_unit_array(void);
|
|
void suite_unit_pointer(void);
|
|
|
|
typedef struct {
|
|
const char *name; /* 套件名,与 CTest 中注册的名字一致 */
|
|
tf_suite_fn fn;
|
|
const char *desc; /* 教材知识点说明 */
|
|
} test_suite_t;
|
|
|
|
static const test_suite_t g_suites[] = {
|
|
{ "integration.system", suite_integration_system, "集成:系统全流程(初始化→播种→采样→命令→存档→重载→自检)" },
|
|
{ "integration.fault", suite_integration_fault, "集成:异常注入与容错(硬件故障/脏数据/边界)" },
|
|
{ "unit.string", suite_unit_string, "项目五 字符数组与字符串处理" },
|
|
{ "unit.num", suite_unit_num, "项目二 数据类型、运算符与类型转换" },
|
|
{ "unit.branch", suite_unit_branch, "项目三 分支结构与条件判断" },
|
|
{ "unit.loop", suite_unit_loop, "项目四 循环结构与嵌套" },
|
|
{ "unit.bitops", suite_unit_bitops, "项目九 位运算与位标志" },
|
|
{ "unit.errors", suite_unit_errors, "项目十 错误处理与错误码" },
|
|
{ "unit.array", suite_unit_array, "项目五 一维/二维数组与字符数组" },
|
|
{ "unit.file", suite_unit_file, "项目九 文件与配置存储" },
|
|
{ "unit.pointer", suite_unit_pointer, "项目七 指针、出参与函数指针" },
|
|
{ "unit.func", suite_unit_func, "项目六 函数、递归与 static 生命周期" },
|
|
{ "unit.struct", suite_unit_struct, "项目八 结构体、共用体与位域" },
|
|
{ "unit.protocol", suite_unit_protocol, "通信帧与 CRC 校验" },
|
|
{ "unit.hal", suite_unit_hal, "GPIO/I2C/UART 硬件抽象层" },
|
|
{ "unit.json", suite_unit_json, "JSON 序列化与解析" },
|
|
{ "integration.http", suite_integration_http, "集成:HTTP 接口端到端" },
|
|
{ "integration.http_api", suite_integration_http_api, "集成:HTTP 接口第二组与错误路径" },
|
|
{ "integration.menu", suite_integration_menu, "集成:菜单/命令/参数解析/回放" },
|
|
};
|
|
|
|
#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;
|
|
}
|