C++ bazel测试与直接执行

C++ bazel测试与直接执行,c++,googletest,bazel,C++,Googletest,Bazel,我正在与bazel一起使用,当我运行bazel测试:tokens\u test(下面定义的规则)时,我有一个失败的测试,但是当我运行编译的测试时,我看到了预期的结果。它失败,因为测试无法打开测试数据文件。我正在运行的测试的目录布局和生成规则如下所示: tokens/ BUILD tokens_test.cpp test_data/ test_input1.txt cc\u测试( name=“令牌测试”, srcs=[“tokens_test.cpp”], deps=[ “@

我正在与bazel一起使用,当我运行
bazel测试:tokens\u test
(下面定义的规则)时,我有一个失败的测试,但是当我运行编译的测试时,我看到了预期的结果。它失败,因为测试无法打开测试数据文件。我正在运行的测试的目录布局和生成规则如下所示:

tokens/
  BUILD
  tokens_test.cpp
  test_data/
    test_input1.txt
cc\u测试(
name=“令牌测试”,
srcs=[“tokens_test.cpp”],
deps=[
“@gtest/:gtest”
“@gtest/:gtest_main”,
],
data=[“//令牌/test\u数据:test\u input1.txt”]
)
此时,测试只是一个包装器,用于打开文件并读取失败的测试数据

TEST(标记器、OpenFileTest){
auto fin=std::ifstream(“test_data/test_input1.txt”);
std::cout将数据定义为

data=[“test\u data/test\u input1.txt”]
使用绝对路径:
/tokens/test\u data/test\u input1.txt

要找到它,请使用
bazel测试--sandbox\u debug-s
运行测试。最后一个命令如下所示:

SUBCOMMAND: # //:hello_test [action 'Testing //:hello_test', configuration: faff19e6fd939f490ac11578d94024c6b7a032836cde039fd5edd28b838194e8, execution platform: @local_config_platform//:host]

(cd /home/s/.cache/bazel/_bazel_s/fa4c7c7c7db2888182e4f15990b55d58/execroot/com_google_absl_hello_world && \
  exec env - \
    EXPERIMENTAL_SPLIT_XML_GENERATION=1 \
    RUNFILES_DIR=bazel-out/k8-fastbuild/bin/hello_test.runfiles \
    RUN_UNDER_RUNFILES=1 \
    TEST_BINARY=./hello_test \
    TEST_INFRASTRUCTURE_FAILURE_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.infrastructure_failure \
    TEST_LOGSPLITTER_OUTPUT_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.raw_splitlogs/test.splitlogs \
    TEST_PREMATURE_EXIT_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.exited_prematurely \
    TEST_SIZE=medium \
    TEST_SRCDIR=bazel-out/k8-fastbuild/bin/hello_test.runfiles \
    TZ=UTC \
    XML_OUTPUT_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.xml \
  external/bazel_tools/tools/test/test-setup.sh ./hello_test)
然后,您可以将整个
(cd…
)粘贴到一行,以在
bazel测试运行期间复制精确的沙箱环境。例如,您可以用这种方式替换最后一行:

(cd /home/s/.cache/bazel/_bazel_s/fa4c7c7c7db2888182e4f15990b55d58/execroot/com_google_absl_hello_world && \
  exec env - \
    EXPERIMENTAL_SPLIT_XML_GENERATION=1 \
    RUNFILES_DIR=bazel-out/k8-fastbuild/bin/hello_test.runfiles \
    RUN_UNDER_RUNFILES=1 \
    TEST_BINARY=./hello_test \
    TEST_INFRASTRUCTURE_FAILURE_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.infrastructure_failure \
    TEST_LOGSPLITTER_OUTPUT_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.raw_splitlogs/test.splitlogs \
    TEST_PREMATURE_EXIT_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.exited_prematurely \
    TEST_SIZE=medium \
    TEST_SRCDIR=bazel-out/k8-fastbuild/bin/hello_test.runfiles \
    TZ=UTC \
    XML_OUTPUT_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.xml \
  bash -c 'pwd && ls')
因此
bash-c'pwd&&ls'
将显示当前目录路径和内容

SUBCOMMAND: # //:hello_test [action 'Testing //:hello_test', configuration: faff19e6fd939f490ac11578d94024c6b7a032836cde039fd5edd28b838194e8, execution platform: @local_config_platform//:host]

(cd /home/s/.cache/bazel/_bazel_s/fa4c7c7c7db2888182e4f15990b55d58/execroot/com_google_absl_hello_world && \
  exec env - \
    EXPERIMENTAL_SPLIT_XML_GENERATION=1 \
    RUNFILES_DIR=bazel-out/k8-fastbuild/bin/hello_test.runfiles \
    RUN_UNDER_RUNFILES=1 \
    TEST_BINARY=./hello_test \
    TEST_INFRASTRUCTURE_FAILURE_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.infrastructure_failure \
    TEST_LOGSPLITTER_OUTPUT_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.raw_splitlogs/test.splitlogs \
    TEST_PREMATURE_EXIT_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.exited_prematurely \
    TEST_SIZE=medium \
    TEST_SRCDIR=bazel-out/k8-fastbuild/bin/hello_test.runfiles \
    TZ=UTC \
    XML_OUTPUT_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.xml \
  external/bazel_tools/tools/test/test-setup.sh ./hello_test)
(cd /home/s/.cache/bazel/_bazel_s/fa4c7c7c7db2888182e4f15990b55d58/execroot/com_google_absl_hello_world && \
  exec env - \
    EXPERIMENTAL_SPLIT_XML_GENERATION=1 \
    RUNFILES_DIR=bazel-out/k8-fastbuild/bin/hello_test.runfiles \
    RUN_UNDER_RUNFILES=1 \
    TEST_BINARY=./hello_test \
    TEST_INFRASTRUCTURE_FAILURE_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.infrastructure_failure \
    TEST_LOGSPLITTER_OUTPUT_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.raw_splitlogs/test.splitlogs \
    TEST_PREMATURE_EXIT_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.exited_prematurely \
    TEST_SIZE=medium \
    TEST_SRCDIR=bazel-out/k8-fastbuild/bin/hello_test.runfiles \
    TZ=UTC \
    XML_OUTPUT_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.xml \
  bash -c 'pwd && ls')