GoogleTest CMake and Make tests fail

I admit that I have a unique situation. We create our application using Make. But my IDE, CLion, uses CMake. So I tried to set up GoogleTest to both (view). I can compile my code both ways (using make on the command line and building from my IDE). But from within CLion, when I select a test device and click the run button, no tests were found and this is what I get:

Running main() from gtest_main.cc  
[==========] Running 0 tests from 0 test cases.  
[==========] 0 tests from 0 test cases ran. (0 ms total)  
[ PASSED  ] 0 tests.

Process finished with exit code 0

      

Here is my test armature:

#include <gtest/gtest.h>
#include "OPProperties.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, ThisTestWillPass) {
        EXPECT_EQ(0, 0);
    }

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


}  // namespace


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

      

Here is my CMakeLists.txt file:

cmake_minimum_required(VERSION 2.8)
project(oneprint)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -lgtest")
add_definitions(-DBOOST_LOG_DYN_LINK)


set(SOURCE_FILES
    src/controllers/OPProperties.cpp
    src/controllers/OPProperties.h
    src/main.cpp)

include_directories(src/controllers)

set(BOOST_ROOT "/usr/local/lib")
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
unset(Boost_INCLUDE_DIR CACHE)
unset(Boost_LIBRARY_DIRS CACHE)
#set(Boost_LIBRARY_DIR /usr/local/arm/lib)
set(OpenCV_LIBRARY_DIR /usr/include/opencv2)
set(Innovatrics_LIBRARY_DIR /usr/local/arm/lib)

find_package(OpenCV REQUIRED)

find_package(Boost 1.57.0 COMPONENTS filesystem thread log chrono system atomic program_options REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})

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

add_executable(OnePrint ${SOURCE_FILES})

target_link_libraries(OnePrint ${OpenCV_LIBS})
target_link_libraries(OnePrint ${Boost_LIBRARIES})
target_link_libraries(OnePrint ${Innovatrics_LIBRARY_DIR})
target_link_libraries(OnePrint ${GTEST_BOTH_LIBRARIES})
target_link_libraries(OnePrint pthread)

      

I added a folder under src called tests, which contains my OPPropertiesTestTest test arm. I also added a test folder at the top level. This folder includes the Makefile and Srcs.mak.

Here is the Makefile:

TARGET = oneprint
BASE = ../

-include $(BASE)Defs.x86.mak
-include $(BASE)OpenCV.mak
-include $(BASE)Boost.mak
-include $(BASE)Innovatrics.mak
-include $(BASE)GTest.mak

-include $(BASE)Incl.mak
-include Srcs.mak

-include $(BASE)Common.mak
-include $(BASE)App.mak

      

Here is the Srcs.mak file:

VPATH = \
../src/controllers:\
../src:\
../src/tests

CPP_SRCS = \
OPProperties.cpp \

# test files
CPP_SRCS += \
OPPropertiesTest.cpp

      

+3


source to share


1 answer


I usually didn't expect makefiles to be mixed with a CMake-managed project; looks like you have a pretty complex setup?

As an aside, I think the root cause here is likely to be twofold. I don't think the test executable is /src/tests/

actually being built. I don't see anything in your CMakelists.txt that would cause this to be built, so unless you do something extra that you didn't show us, the test files won't compile.

What probably looks like it is, is that you link the helper lib that gtest provides into your target OnePrint

. This helper library is unusual in that it implements a function main()

so that users can specify their own.

You do it in line

target_link_libraries(OnePrint ${GTEST_BOTH_LIBRARIES})

      

From > docs GTEST_BOTH_LIBRARIES

is a variable containing both libgtest and libgtest-main. You only need libgtest as you wrote your own main()

. Therefore, you must use GTEST_LIBRARIES

.


Note. There are several other issues with CMake code:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -lgtest")

      

It's not cross-platform (which may not matter to you) for example. MSVC does not recognize any of these flags. If it matters, you need to wrap it in a block if(UNIX)

or if(NOT MSVC)

.

Also, this sets the compiler flags globally, i.e. for every goal defined in your project. If you need finer-scale controls, see which allows you to specify them for each goal. target_compile_options

The other problem is part -lgtest

. This is really a call for the linker to link the gtest library. However, CMake will take care of this for you - you do it when you call target_link_libraries(OnePrint ${GTEST_LIBRARIES})

.




add_definitions(-DBOOST_LOG_DYN_LINK)

      

While this is good, the same comments on the subject apply globally. Equivalent to each goal . target_compile_definitions


set(BOOST_ROOT "/usr/local/lib")

      

This might be the location of Boost on your machine, but it might not be the case for other developers. This variable is really meant to be specified by individual users when invoking CMake (if needed ... many paths are automatically looked for by CMake) - you shouldn't really hard-code paths like this into the CMake file.


target_link_libraries(OnePrint pthread)

      

Again, pthread

not a good reference if you are using MSVC; this should probably be wrapped in a block if

.


The end trivial point is that you can specify more than one dependency in a command target_link_libraries

, so you can change them to:

if(NOT MSVC)
  set(PThreadLib pthread)
endif()
target_link_libraries(OnePrint
    ${OpenCV_LIBS}
    ${Boost_LIBRARIES}
    ${Innovatrics_LIBRARY_DIR}
    ${GTEST_LIBRARIES}
    ${PThreadLib})

      

+3


source







All Articles