Appearance
Codelab: 编写第一个测试
预计时间:15 分钟
学习目标
完成本教程后,你将能够:
- 创建 Anvil 测试项目结构
- 编写基本测试用例
- 使用构建选项注释
- 运行测试并查看结果
步骤 1: 创建项目结构
创建以下目录结构:
bash
mkdir -p calculator-demo/{src,include,build,tests}
cd calculator-demo目录结构:
calculator-demo/
├── src/
│ └── calculator.c # 源代码
├── include/
│ └── calculator.h # 头文件
├── build/
│ └── calculator.o # 编译产物
└── tests/
└── test_calculator.c # 测试文件步骤 2: 编写被测代码
创建头文件 include/calculator.h:
c
#ifndef CALCULATOR_H
#define CALCULATOR_H
// 加法
int calc_add(int a, int b);
// 减法
int calc_subtract(int a, int b);
// 乘法
int calc_multiply(int a, int b);
// 除法(除零返回 0)
int calc_divide(int a, int b);
#endif // CALCULATOR_H创建源文件 src/calculator.c:
c
#include "calculator.h"
int calc_add(int a, int b) {
return a + b;
}
int calc_subtract(int a, int b) {
return a - b;
}
int calc_multiply(int a, int b) {
return a * b;
}
int calc_divide(int a, int b) {
if (b == 0) {
return 0; // 安全处理除零
}
return a / b;
}步骤 3: 编译源代码
bash
gcc -c src/calculator.c -o build/calculator.o -Iinclude验证编译成功:
bash
ls build/
# 应显示: calculator.o步骤 4: 编写测试用例
创建测试文件 tests/test_calculator.c:
c
#include "calculator.h"
/*
* 设计说明:测试基本加法功能
* 预期结果:3 + 5 应返回 8
*/
__attribute__((test_method))
int test_add_basic(void) {
int result = calc_add(3, 5);
return result == 8;
}
/*
* 设计说明:测试负数加法
* 预期结果:-3 + 5 应返回 2
*/
__attribute__((test_method))
int test_add_negative(void) {
int result = calc_add(-3, 5);
return result == 2;
}
/*
* 设计说明:测试基本减法功能
* 预期结果:10 - 4 应返回 6
*/
__attribute__((test_method))
int test_subtract_basic(void) {
int result = calc_subtract(10, 4);
return result == 6;
}
/*
* 设计说明:测试基本乘法功能
* 预期结果:6 × 7 应返回 42
*/
__attribute__((test_method))
int test_multiply_basic(void) {
int result = calc_multiply(6, 7);
return result == 42;
}
/*
* 设计说明:测试正常除法
* 预期结果:20 ÷ 4 应返回 5
*/
__attribute__((test_method))
int test_divide_basic(void) {
int result = calc_divide(20, 4);
return result == 5;
}
/*
* 设计说明:测试除零边界情况
* 预期结果:除以零应返回 0(安全处理)
*/
__attribute__((test_method))
int test_divide_by_zero(void) {
int result = calc_divide(10, 0);
return result == 0;
}
// 指定头文件搜索路径
// includes: -I../include步骤 5: 运行测试
bash
anvil预期输出:
Discovered 6 test cases in 1 file.
test_calculator::test_add_basic ........ PASSED
test_calculator::test_add_negative ..... PASSED
test_calculator::test_subtract_basic ... PASSED
test_calculator::test_multiply_basic ... PASSED
test_calculator::test_divide_basic ..... PASSED
test_calculator::test_divide_by_zero ... PASSED
All 6 tests passed.步骤 6: 添加初始化和清理
修改 tests/test_calculator.c,在文件开头添加:
c
#include "calculator.h"
#include <stdio.h>
static int test_count = 0;
__attribute__((test_initialize))
int setup(void) {
test_count++;
printf("[setup] preparing test #%d\n", test_count);
return 0; // 0 = 成功
}
__attribute__((test_cleanup))
int teardown(void) {
printf("[cleanup] test #%d complete\n", test_count);
return 0;
}
// ... 其余测试函数 ...再次运行:
bash
anvil现在每个测试都会显示初始化和清理日志。
步骤 7: 生成测试报告
生成 JUnit XML 报告:
bash
anvil -o report.xml查看报告:
bash
cat report.xml步骤 8: 运行单个测试
只运行特定测试:
bash
# 运行 test_add_basic
anvil test_calculator/test_add_basic输出:
test_calculator::test_add_basic ........ PASSED知识点总结
你已学会:
| 技能 | 说明 |
|---|---|
| 项目结构 | src/, include/, build/, tests/ |
| 测试属性 | __attribute__((test_method)) |
| 返回值 | 非零=通过, 0=失败 |
| 构建选项 | // includes: 注释 |
| 初始化/清理 | test_initialize, test_cleanup |
| 运行测试 | anvil 命令 |
| 过滤测试 | anvil test_file/test_func |
完整代码
最终的项目结构:
calculator-demo/
├── src/
│ └── calculator.c
├── include/
│ └── calculator.h
├── build/
│ └── calculator.o
├── tests/
│ └── test_calculator.c
└── report.xml下一步
- Codelab: Mock 硬件依赖 - 学习使用 Mock 进行隔离测试