[assert_continues]
fn abc(ii int) {
assert ii == 2
}
fn test_abc() {
for i in 0 .. 4 {
abc(i)
}
}
输出所有断言不通过的表达式值:
main_test.v:3: ✗ fn abc
> assert ii == 2
Left value: 0
Right value: 2
main_test.v:3: ✗ fn abc
> assert ii == 2
Left value: 1
Right value: 2
main_test.v:3: ✗ fn abc
> assert ii == 2
Left value: 3
Right value: 2
也可以使用命令行参数来实现相同的效果,而不用每个函数都加assert_continues注解。
v -assert continues test .
另外-assert也有别的选项:
v -assert aborts test . #默认的行为,碰到断言不通过,停止测试
v -assert backtraces test . #碰到断言不通过,停止测试,并调用print_backtrace()显示堆栈信息
执行测试
执行单个测试文件:
v test xxx_test.v
执行模块测试文件:
v test xxx(模块名/目录名)
会逐个执行模块,包括子模块中的所有测试文件,所有以test_开头的测试函数。
执行测试时,可增加-stats选项,显示更为详细的测试结果。
v -stats test xxx.v
忽略testdata目录
如果希望有一些测试文件要忽略执行,可以创建名为testdata的目录,测试框架会忽略这个目录。
实际开发场景中,这个目录还是挺实用的,可以把临时不用的测试文件挪到该目录中。
特定操作系统测试
如果希望有些测试函数只在特定操作系统下执行,可以使用if注解来实现。
执行测试的时候,只有满足操作系统条件的测试函数才执行。
[if macos] //只有macos系统中才执行测试
pub fn test_in_macos() {
println("test in macos")
assert true
}
[if !macos] //只有非macos系统才执行测试
pub fn test_not_in_macos() {
$if !macos {
println("test not in macos")
}
$if macos {
println("test in macos")
}
println("test only not in macos")
assert false
}
[if linux]
pub fn test_only_in_linux() {
println("test only in linux")
assert false
}
[if linux || windows] //在linux或windows系统中才执行测试
pub fn test_in_linux_or_macos() {
println("test in linux or macos")
assert false
}