Appearance
快速入门
5 分钟体验 Anvil 测试框架。
安装 Anvil
从源码编译安装:
bash
git clone https://git.xahwsys.com/tools/anvil.git
cd anvil
go build -o anvil ./cmd/anvil
sudo mv anvil /usr/local/bin/验证安装:
bash
anvil --version创建项目结构
创建以下目录结构:
my-project/
├── src/
│ └── math.c # 源代码
├── include/
│ └── math.h # 头文件
├── build/
│ └── math.o # 编译产物
└── tests/
└── test_math.c # 测试文件编写被测代码
include/math.h:
c
#ifndef MATH_H
#define MATH_H
int add(int a, int b);
int divide(int a, int b);
#endifsrc/math.c:
c
#include "math.h"
int add(int a, int b) {
return a + b;
}
int divide(int a, int b) {
if (b == 0) return 0;
return a / b;
}编译源代码
bash
gcc -c src/math.c -o build/math.o -Iinclude编写第一个测试
tests/test_math.c:
c
#include "math.h"
/*
* 设计说明:测试基本加法功能
* 预期结果:2 + 3 应返回 5
*/
__attribute__((test_method))
int test_add_basic(void) {
return add(2, 3) == 5;
}
/*
* 设计说明:测试负数加法
* 预期结果:-1 + 1 应返回 0
*/
__attribute__((test_method))
int test_add_negative(void) {
return add(-1, 1) == 0;
}
/*
* 设计说明:测试除零边界情况
* 预期结果:除以零应返回 0(安全处理)
*/
__attribute__((test_method))
int test_divide_by_zero(void) {
return divide(10, 0) == 0;
}
// includes: -I../include关键点
__attribute__((test_method))标记测试函数- 返回非零值 = 测试通过,返回 0 = 测试失败
// includes:注释指定头文件搜索路径
运行测试
bash
anvil预期输出:
Discovered 3 test cases in 1 file.
test_math::test_add_basic .......... PASSED
test_math::test_add_negative ....... PASSED
test_math::test_divide_by_zero ..... PASSED
All 3 tests passed.生成测试报告
生成 JUnit XML 格式报告:
bash
anvil -o report.xml下一步
- 命令行使用 - 了解完整的命令行选项
- 配置文件 - 使用
.anvil配置文件 - 测试方法语法 - 详细了解测试定义
- Codelab: 编写第一个测试 - 完整的实践教程