Search code examples
c++cmakegoogletestclion

Google Test with CLion isn't running tests


I am trying to get a simple test to run with google test and CLion. I think I have cmake setup properly and I am able to "run a test". When I do it says no tests were found. Any ideas? CMakeLists.txt

cmake_minimum_required(VERSION 3.7)

project(expirement)

find_package(Threads REQUIRED)

set(CMAKE_CXX_STANDARD 14)

set(SOURCE_FILES main.cpp getstring.cpp tests.cpp)
include(${CMAKE_ROOT}/Modules/ExternalProject.cmake)

ExternalProject_Add(googletest
        SOURCE_DIR "/home/dcooke/googletest-master/googletest/"
        )
ExternalProject_Get_Property(googletest source_dir)
include_directories(${source_dir}/include)

add_executable(expirement main.cpp getstring.cpp tests.cpp)
add_dependencies(expirement googletest)



target_link_libraries(expirement
        /home/dcooke/BuildBinaries/lib/libgtest.a
        /home/dcooke/BuildBinaries/lib/libgtest_main.a
        )

target_link_libraries(expirement Threads::Threads)

enable_testing()

The tests file:

#include "getstring.h"
#include "gtest/gtest.h"

TEST(StringTest, CheckReturnValue) {
    ASSERT_EQ("asdfasf",GetTheString());
}

The command line that and results:

/home/user/Code/expirement/cmake-build-debug/expirement --gtest_filter=StringTest.*:StringTest/*.*:*/StringTest.*/*:*/StringTest/*.* --gtest_color=no
Testing started at 10:18 AM ...
The String

Process finished with exit code 0
Empty test suite.

Solution

  • Other comments and answers suggest that you need to use add_test(). You only need to use enable_testing() and add_test() if you want to use convenience of CMake's CTest framework. Any executable can be a test if you want it to be. Let's dig into your problem.

    First, simplify. I assume you have googletest built successfully, and you know how to use it (If not, that's a separate question.) Reduce your CMakeLists.txt.

    CMakeLists.txt

    cmake_minimum_required(VERSION 3.7)
    project(expirement)
    
    find_package(Threads REQUIRED)
    set(CMAKE_CXX_STANDARD 14)
    
    set(SOURCE_FILES main.cpp getstring.cpp tests.cpp)
    include_directories($ENV{HOME}/googletest/googletest/include)
    
    add_executable(expirement main.cpp getstring.cpp tests.cpp)
    target_link_libraries(expirement
            $ENV{HOME}/googletest/googlemock/gtest/libgtest.a
            $ENV{HOME}/googletest/googlemock/gtest/libgtest_main.a
            )
    target_link_libraries(expirement Threads::Threads)
    

    You'll notice I dropped the ExternalProject bits, and you can figure out where I put googletest.

    Now, since we don't have your source I had to mock a couple of stubs.

    main.cpp

    #include "gtest/gtest.h"
    
    int main(int argc, char *argv[]) {
      ::testing::InitGoogleTest(&argc, argv);
      return RUN_ALL_TESTS();
    }
    

    getstring.h

    #include <string>
    
    std::string GetTheString();
    

    getstring.cpp

    #include <string>
    
    std::string GetTheString() {
      return (std::string) "asdfasf";
    }
    

    Now, cmake and build:

    ~/foo
    ❯ cmake .
    -- The C compiler identification is AppleClang 8.1.0.8020038
    -- The CXX compiler identification is AppleClang 8.1.0.8020038
    -- Check for working C compiler: /App
    [ ... cmake output goes here. blah blah blah ... ]
    -- Found Threads: TRUE  
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /Users/nega/foo
    
    ~/foo
    ❯ make
    Scanning dependencies of target expirement
    [ 25%] Building CXX object CMakeFiles/expirement.dir/main.cpp.o
    [ 50%] Building CXX object CMakeFiles/expirement.dir/getstring.cpp.o
    [ 75%] Building CXX object CMakeFiles/expirement.dir/tests.cpp.o
    [100%] Linking CXX executable expirement
    [100%] Built target expirement
    
    ~/foo
    ❯ 
    

    Now run experiment, but simplify. (Also, remember to escape those *'s!)

    ~/foo
    ❯ ./expirement --gtest_filter='*'
    [==========] Running 1 test from 1 test case.
    [----------] Global test environment set-up.
    [----------] 1 test from StringTest
    [ RUN      ] StringTest.CheckReturnValue
    [       OK ] StringTest.CheckReturnValue (0 ms)
    [----------] 1 test from StringTest (0 ms total)
    
    [----------] Global test environment tear-down
    [==========] 1 test from 1 test case ran. (0 ms total)
    [  PASSED  ] 1 test.
    
    ~/foo
    ❯ 
    

    If you get to this point, great! You're well on your way. You can build up a tighter filter for --gtest_filter. (Remember to escape those *'s!) You can build up your CMakeLists.txt to include those ExternalProject bits that you took out. Remember though, the real power and utility in ExternalProject is when you want to build an external project as part of the build process for your project. Often times it's much simpler to direct your users to download and build that external project themselves.

    If you did not get to this point, stop. Take a step back and look into your code. Ensure that you have your tests setup and defined correctly. Ensure that you're initializing googletest correctly. Make sure you don't have any spelling errors in your test names. (A common "gotcha".) Get that part fixed, and then come back to the CMake part.