【问题标题】:GoogleTest CMake doesn't recognize TEST_F: Like it's not recognizing GTest somethingGoogleTest CMake 无法识别 TEST_F:就像它无法识别 GTest 一样
【发布时间】:2015-05-06 18:00:31
【问题描述】:

好吧,我承认,这是一个独特的案例。当我们构建我们的应用程序时,我们正在使用 make,所以我将我的测试包含在 src 下的测试文件夹中。然后在与我们的发布文件夹相同的级别上,我们创建了一个单元测试文件夹,其中包含我们所有的源文件和我们的测试源文件。

但我的 IDE 是 CLion,它使用 CMake。在我的 CMakeLists.txt 文件中,我包含了:

enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

add_executable(TestProject ${SOURCE_FILES})

target_link_libraries(TestProject ${GTEST_BOTH_LIBRARIES})

我正在创建我的第一个测试夹具。代码如下:

#include "OPProperties.h"
#include "gtest/gtest.h"

namespace {
// The fixture for testing class OPPropertiesTestTest.
    class OPPropertiesTestTest : public ::testing::Test {
    protected:
        // You can remove any or all of the following functions if its body
        // is empty.

        OPPropertiesTestTest() {
            // You can do set-up work for each test here.
        }

        virtual ~OPPropertiesTestTest() {
            // You can do clean-up work that doesn't throw exceptions here.
        }

        // If the constructor and destructor are not enough for setting up
        // and cleaning up each test, you can define the following methods:

        virtual void SetUp() {
            // Code here will be called immediately after the constructor (right
            // before each test).
        }

        virtual void TearDown() {
            // Code here will be called immediately after each test (right
            // before the destructor).
        }

        // Objects declared here can be used by all tests in the test case for OPPropertiesTestTest.
    };


    TEST_F(OPPropertiesTestTest, ThisTestWillFail) {
        EXPECT_EQ(0, 2);
    }

}  // namespace


int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

这是一张图片截图:

请注意我的 TEST_F 函数中的语法检查器错误。当我开始键入 TEST_F 时,代码完成正在尝试查找 Boost 测试函数。

谁能告诉我我还需要添加到 CMakeLists.txt 文件中的哪些内容,或者我没有做哪些 GTest 函数未被识别?

【问题讨论】:

  • 哦,我对 CMake 的 find_package 相当不熟悉。事实上,我在使用 GTest 时非常失败。但是你的测试用例源代码对我来说看起来还不错。 “注意语法检查器错误” 这段代码有实际的编译错误吗? GoogleTest 和 GoogleMock 使用了一些 esoteric 模板技术,它们非常适合混淆几个 IDE 的索引器和智能代码检查器(例如,在使用 Eclipse CDT Luna 处理我的测试用例时,我经常遇到后者)。

标签: linux cmake makefile googletest


【解决方案1】:

正如 πάντα ῥεῖ 所指出的,我实际上并没有尝试构建代码。当我第一次收到 pthread 的链接器错误时,我们在 CMakeLists.txt 文件中添加了以下行:

target_link_libraries(OnePrint pthread)

然后我再次尝试构建并收到这些错误:

/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_key_create'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_getspecific'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_key_delete'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status

所以,我对这些错误进行了搜索,发现this question

对我有用的答案是here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 2021-03-30
    相关资源
    最近更新 更多