⚡
极简测试发现
使用 __attribute__((test_method)) 标记,无需手动注册测试用例,Anvil 自动发现并执行。
告别繁琐的测试注册。只需使用 __attribute__((test_method)) 标记函数,Anvil 就能自动发现并执行测试。
__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 # 你只需编写测试通过链接器 --wrap 机制拦截函数调用,无需预处理器宏。支持两种模式:
// 用户实现模式
__attribute__((mock))
int read_sensor(int id) {
return 42; // 自定义返回值
}
// 声明式模式
__attribute__((mock))
int read_sensor(int id);
mock(read_sensor).set_return(42); // 动态配置一个测试函数,自动生成多个边界值用例:
// 自动展开为 8 个测试用例
__attribute__((test_method, params(n = boundary(uint8))))
int test_byte_boundary(int n) {
return validate_byte(n) == (n >= 0 && n <= 255);
}