Skip to content

Anvil™现代 C 测试框架

极简注册、自动依赖、强大 Mock

⚡ 极简测试发现

告别繁琐的测试注册。只需使用 __attribute__((test_method)) 标记函数,Anvil 就能自动发现并执行测试。

c
__attribute__((test_method))
int test_add(void) {
    return add(2, 3) == 5;  // 返回非零 = 通过
}

🔗 全自动依赖解析

无需编写复杂的 Makefile 或链接脚本。Anvil 使用 nm -u 分析符号依赖,自动链接所需的 .o 文件。

project/
├── src/
│   └── math.c
├── build/
│   └── math.o        # Anvil 自动链接
└── tests/
    └── test_math.c   # 你只需编写测试

🎭 强大的 Mock 系统

通过链接器 --wrap 机制拦截函数调用,无需预处理器宏。支持两种模式:

c
// 用户实现模式
__attribute__((mock))
int read_sensor(int id) {
    return 42;  // 自定义返回值
}

// 声明式模式
__attribute__((mock))
int read_sensor(int id);

mock(read_sensor).set_return(42);  // 动态配置

📊 参数化边界测试

一个测试函数,自动生成多个边界值用例:

c
// 自动展开为 8 个测试用例
__attribute__((test_method, params(n = boundary(uint8))))
int test_byte_boundary(int n) {
    return validate_byte(n) == (n >= 0 && n <= 255);
}

本页面内容遵循 Luna 软件源代码授权条款 (LSLA) 发布