How to pass custom flags to `bazel test` command
Asked Answered
P

2

9

I have used gflags in my test to define custom flags. How can I pass such a flag to my test while running the test via bazel test command?

For example: I can run a test multiple times using:

bazel test //xyz:my_test --runs_per_test 10 

In the same command I would like to pass a flag defined in my_test say --use_xxx, how do I do so?

Perihelion answered 15/6, 2018 at 14:28 Comment(2)
I think the word flags might be overloaded. Do you mean arguments to the main method of your test runner?Thrifty
it's interesting here because with bazel run you can leverage the fact that everything to the right of a lone -- would get interpreted as arguments to pass down to the process while for build and run commands that blocked off section functions as target patterns as in bazel [<startup options>] <command> [<args>] -- [<target patterns>]Buckskins
R
15

Use the --test_arg flag.

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

From the docs:

--test_arg arg: Passes command-line options/flags/arguments to each test process. This option can be used multiple times to pass several arguments, e.g. --test_arg=--logtostderr --test_arg=--v=3.

You can also specify arguments for the test as part of the BUILD definition:

cc_test(
    name = "my_test",
    srcs = [".."],
    deps = [".."],
    args = ["--use_xxx", "--some_number=42"],
)
Rile answered 16/6, 2018 at 18:4 Comment(0)
A
-1

You can add a main into your test. It will look like this.

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();
} 

It works fine for me.

Advantage answered 17/11, 2022 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.