Valar Morghulis
Valar Morghulis

Reputation: 687

Googletest never fails (where it should) with bazel test but works with cmake & clion

I am trying to use googletest with bazel and cmake.

The cmake and clion works perfect as for the test part, it fails where it should fail and passes where it should pass. However, bazel test will pass all the tests even they should not.

Bazel test not working

For example, I have the stupid_test.cc file:

#include "gtest/gtest.h"

TEST(StupidTests, Stupid1) {
  EXPECT_EQ(100, 20);
  EXPECT_TRUE(false);
}

which should fail always.

The bazel BUILD file:

cc_test(
    name = "stupid_test",
    srcs = ["stupid_test.cc"],
    deps = [
        "//bazel_build/googletest:gtest",
    ],
)

where bazel_build/googletest is exactly the same files downloaded from the googletest github.

Then I run bazel test :stupid_test, the output is:

⇒  blaze test :stupid_test
DEBUG: /private/var/tmp/_bazel_myusername/741c62b201e51840aa320b156e05fd70/external/bazel_tools/tools/osx/xcode_configure.bzl:87:9: Invoking xcodebuild failed, developer dir: /Users/honghaoli/Downloads/Xcode.app/Contents/Developer ,return code 1, stderr: xcrun: error: invalid DEVELOPER_DIR path (/Users/honghaoli/Downloads/Xcode.app/Contents/Developer), missing xcrun at: /Users/honghaoli/Downloads/Xcode.app/Contents/Developer/usr/bin/xcrun
, stdout: 
INFO: Analysed target //:stupid_test (0 packages loaded, 0 targets configured).
INFO: Found 1 test target...
Target //:stupid_test up-to-date:
  bazel-bin/stupid_test
INFO: Elapsed time: 1.840s, Critical Path: 1.55s
INFO: 4 processes: 4 darwin-sandbox.
INFO: Build completed successfully, 4 total actions
//:stupid_test                                                           PASSED in 0.1s

Executed 1 out of 1 test: 1 test passes.
INFO: Build completed successfully, 4 total actions

I have no idea why it passed.

CMake Works

The same test file would fail with very clear message using cmake and clion. For example, part of the CMakeLists

add_executable(stupid_test stupid_test.cc)
target_link_libraries(stupid_test gtest gtest_main)
add_test(NAME stupid_test COMMAND stupid_test)

and the output fail message:

Testing started at 19:16 ...
/Users/myusername...blabla/cmake-build-debug/stupid_test --gtest_filter=* --gtest_color=no
Running main() from /Users/myusername...blabla/cmake-build-debug/googletest-src/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite./Users/myusername...blabla/stupid_test.cc:11: Failure
Expected equality of these values:
  100
  20
/Users/myusername...blabla/stupid_test.cc:12: Failure
Value of: false
  Actual: false
Expected: true

/Users/myusername...blabla/stupid_test.cc:13: Failure
Value of: true
  Actual: true
Expected: false


Process finished with exit code 1

I am using the Mac OS 10.14 if that helps.

Upvotes: 2

Views: 957

Answers (1)

Valar Morghulis
Valar Morghulis

Reputation: 687

I figured out the reason.

Just change

"//bazel_build/googletest:gtest",

in the BUILD file into

"//bazel_build/googletest:gtest_main",

I'm still confused! If the gtest does not work, it should at least fail the build or throw error/warnings. It should never say:

Executed 1 out of 1 test: 1 test passes.

as the output.

I consider it as a bug.

Upvotes: 1

Related Questions