Error running google test on default in CLion

Viewed 1605

So, unfortunatly I am unsure how to properly describe the error message. Essentially I am trying to get used to google test, - I want to use it to test my C++ project in CLion. I create a new library project, with the following classes:

#include "gtest/gtest.h"

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

and also:

#include "gtest/gtest.h"

TEST(MyTestCategory, Vec2DAdditionTest){
    EXPECT_EQ(1, 1);
}

Of course these tests are not useful at all - but its just to see if everything works the way it should. Now when I tried to run them, I am prompted the following error:

6:46 PM Error running 'MyTestCategory.Vec2DAdditionTest': Cannot run 'MyTestCategory.Vec2DAdditionTest' on '<default>'

6:47 PM Error running 'All in main.cpp': Cannot run 'All in main.cpp' on '<default>'

What am I missing? I can't get the tests to run, - neither individually, nor directly over the main function? Also after this, the build/run button gets greyed out in CLion and I have to right click on the main.cpp to force it to run/compile..

Essential I have a project structure like so:

src/
  a.cpp
  b.cpp
  CMakeLists.txt
test/
  main.cpp
  atests.cpp
  CmakeLists.txt
CMakeLists.txt

My run configuration for the test project looks like so:

1 Answers

Here is an example on how you can add GTests in your CLion project:

Consider a project structure very similar to what you have presented, however, with an additional file CMakeLists.txt.in in the test folder.:

src/
  a.cpp
  b.cpp
  CMakeLists.txt
test/
  main.cpp
  atests.cpp
  CMakeLists.txt
  CMakeLists.txt.in
CMakeLists.txt

The CMakeLists.txt.in helps to download and add the GTest libraries to your project while building your project.

The content of CMakeLists.txt.in looks as below:

cmake_minimum_required(VERSION 2.8.2)

project(googletest-download NONE)

include(ExternalProject)
ExternalProject_Add(googletest
  GIT_REPOSITORY    https://github.com/google/googletest.git
  GIT_TAG           master
  SOURCE_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
  BINARY_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
  CONFIGURE_COMMAND ""
  BUILD_COMMAND     ""
  INSTALL_COMMAND   ""
  TEST_COMMAND      ""
)

The test/CMakeLists.txt file looks as below:

cmake_minimum_required(VERSION 3.10)

### START OF CONFIGURING GTEST
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
execute_process(COMMAND ${CMAKE_COMMAND} --build .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )

# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
  ${CMAKE_CURRENT_BINARY_DIR}/googletest-build
  EXCLUDE_FROM_ALL)
### END OF CONFIGURING GTEST

# Now simply link against gtest or gtest_main as needed. Eg

add_executable(test_${PROJECT_NAME} main.cpp atest.cpp)
target_link_libraries(test_${PROJECT_NAME} ${PROJECT_NAME} gtest gtest_main)
add_test(NAME test_PROJECT_NAME COMMAND test_${PROJECT_NAME})

CMakeLists.txt in the root folder has the following content:

cmake_minimum_required(VERSION 3.10)
project(gtestTest)

set(CMAKE_CXX_STANDARD 14)

add_subdirectory(src)
add_subdirectory(test)

Now reload the CMake configuration and try running the test_gtestTest target to run the unit tests. You can also create custom run configurations using gtest template of CLion to get user friendly test reports.

For more information on GTests with CLion, please refer to:

  1. Google Test Support in Clion

  2. Building Google Tests with CMake

Related