在BUILD文件中使用testonly关键字来明确指定哪些targets是test targets。
示例代码:
在BUILD文件中,假设我们有三个targets:my_library、my_binary1、my_binary2。
my_library是一个编译为库的target,而my_binary1和my_binary2是编译为可执行文件的targets。
为了使Bazel test ...只运行my_library的测试,我们可以将其标记为testonly:
cc_library( name = "my_library", srcs = ["my_library.cc"], hdrs = ["my_library.h"], testonly = True, # 标记为testonly )
这样,运行Bazel test ...时,只有标记为testonly的target会被执行测试,而其他target则会被忽略。