GoogleTest CMake doesn't recognize TEST_F: Lik

2019-02-28 23:08发布

问题:

OK, I admit it, this is a unique case. When we build our application we are using make, so I've included my tests in a test folder under src. Then at the same level as our release folder we have created a unit-test folder that includes all of our source files and our test source files.

But my IDE is CLion, which uses CMake. In my CMakeLists.txt file, I have included:

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

add_executable(TestProject ${SOURCE_FILES})

target_link_libraries(TestProject ${GTEST_BOTH_LIBRARIES})

I am creating my first Test Fixture. Here is the code:

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

Here is an image capture:

Notice the syntax checker errors in my TEST_F function. When I started to type TEST_F code completion is trying to find a Boost Test Function.

Can someone tell me what else I need to add to the CMakeLists.txt file or what I am not doing that GTest functions are not being recognized?

回答1:

As πάντα ῥεῖ pointed out, I hadn't actually tried to build the code. When I did I first received a linker error for pthread, so we added the following line the the CMakeLists.txt file:

target_link_libraries(OnePrint pthread)

Then I tried again to build and received these errors:

/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

So, I ran a search on these errors and found this question.

The answer that worked for me was here.