如何将自定义标志传递给"bazel test"命令



我在测试中使用了gflags来定义自定义标志。如何在通过命令运行测试时将这样的标志传递给我的测试bazel test

例如:我可以使用以下方法多次运行测试:

bazel test //xyz:my_test --runs_per_test 10 

在同一个命令中,我想传递一个在my_test中定义的标志,比如说--use_xxx,我该怎么做?

使用--test_arg标志。

bazel test //xyz:my_test --runs_per_test=10 --test_arg=--use_xxx --test_arg=--some_number=42

从文档中:

--test_arg arg:将命令行选项/标志/参数传递给每个测试进程。这 选项可以多次用于传递多个参数,例如--test_arg=--logtostderr --test_arg=--v=3.

您还可以将测试的参数指定为 BUILD 定义的一部分:

cc_test(
name = "my_test",
srcs = [".."],
deps = [".."],
args = ["--use_xxx", "--some_number=42"],
)

您可以在测试中添加 main。它看起来像这样。

TEST(A, FUNC) {
// Your test here.
}
int main(int argc, char** argv) {
gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
} 

它对我来说效果很好。

最新更新