我有一个单元测试,它测试一些多线程代码。当我编写这段代码时,我多次使用了以下命令
bazel test //xyz:my_test --runs_per_test 1000
这暴露了一些并发问题,我现在已经修复了。我想将此
--runs_per_test
值添加到我的 BUILD 文件中的测试定义中,以便夜间构建(jenkins)多次运行此测试。我该怎么做?
这是一个更好的技巧。
因为我的测试是 C++ google 测试;谷歌测试接受一个标志
--gtest_repeat
来重复运行测试;我可以将这个标志从 bazel BUILD 文件的 args
属性 传递给测试
cc_test(
size = "small",
name = "my_test",
srcs = [".."],
deps = [".."],
args = ["--gtest_repeat=10"],
)
这种方法的问题是 bazel 会将所有运行视为单个测试,因此运行时间将是所有运行的累积时间。如果这超过了测试大小的时间限制(本例中为
small
),那么 bazel 将终止测试并将其结果显示为 timed-out
!
这里有一个使用
sh_test
包装器的 hacky 解决方法,假设您有一个 cc_test
目标。我用 Bazel 自己的 //examples/cpp/
作为例子:
# Test to run multiple times
cc_test(
name = "hello-fail_test",
srcs = ["hello-fail.cc"],
deps = [":hello-lib"],
)
# Wrapper to run test multiple times
sh_test(
name = "repeated_hello-fail_test",
srcs = ["hello-fail.sh"],
data = [":hello-fail_test"],
)
并在
hello-fail.sh
中,在 for 循环中运行测试并决定测试失败时您希望执行的操作:
#!/bin/bash
WORKSPACE="$TEST_SRCDIR/io_bazel"
# Or count the number of successes/fails
# SUCCESS_COUNT=0
# FAIL_COUNT=0
for i in {1..3} # set the number of runs
do
$WORKSPACE/examples/cpp/hello-fail_test
if [ $? = 1 ]; then
exit 1 # fail immediately, or increment test failure count
fi
done
exit 0