如何在 BUILD 文件中指定测试应该 运行 的次数

How to specify number of times a test should be run in BUILD file

我有一个单元测试,用于测试一些多线程代码。在编写这段代码时,我多次使用以下命令

bazel test //xyz:my_test --runs_per_test 1000

这暴露了一些我现在已经修复的并发问题。我想将此 --runs_per_test 值添加到我的 BUILD 文件中的测试定义中,以便夜间构建 (jenkins) 多次运行此测试。我该怎么做?

你不能。 我建议在 https://github.com/bazelbuild/bazel/issues

提交功能请求

假设您有一个 cc_test 目标,这里有一个使用 sh_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

这是一个更好的技巧。

因为我的测试是 C++ google tests; google 测试接受标志 --gtest_repeat 以重复 运行ning 测试;我可以将这个标志从 bazel BUILD 文件传递​​给测试,在 args 属性

cc_test(
    size = "small",
    name = "my_test",
    srcs = [".."],
    deps = [".."],
    args = ["--gtest_repeat=10"],
)

这种方法的问题是 bazel 会将所有 运行 视为单个测试,因此 运行 时间将是所有 运行 的累积时间。如果这超过了测试大小的时间限制(本例中为 small),那么 bazel 将终止测试!