Setup Google test in CLion
Asked Answered
D

4

48

I have been sitting online for hours already trying to setup Google test on Clion in Linux but have not been able to find anything.

Can someone guide me with setting this up please?

Decasyllabic answered 10/11, 2015 at 19:56 Comment(1)
Here is the tutorial: youtube.com/watch?v=8Up5eNZ0FLwKendra
T
41

Create new Project

  1. Create a repository in my ClionProjects folder
    • cd ~/ClionProjects
    • mkdir .repo
    • cd .repo
  2. Clone the DownloadProject from github
    • git clone https://github.com/Crascit/DownloadProject.git
  3. Create a C++ project with a src and test directory

Add following files:

CMakeLists.txt

cmake_minimum_required(VERSION 3.3)

project(MyProjectName)

add_subdirectory(src)
add_subdirectory(test)

src/CMakeLists.txt

#set(core_SRCS ADD ALL SOURCE FILES HERE)

add_library(core ${core_SRCS})
add_executable(exe main.cpp)
target_link_libraries(exe core)

[We compile a library so we can include it inside the Test project]

test/CMakeLists.txt

cmake_minimum_required(VERSION 3.3)

set(REPO ~/ClionProjects/.repo)

project(Test)

project(Example)

include(CTest)
enable_testing()

#set(gtest_disable_pthreads on) #needed in MinGW
include(${REPO}/DownloadProject/DownloadProject.cmake)
download_project(
        PROJ                googletest
        GIT_REPOSITORY      https://github.com/google/googletest.git
        GIT_TAG             master
        UPDATE_DISCONNECTED 1
        )

add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL)

#set(test_SRCS ADD ALL TEST SOURCE FILES HERE)
add_executable(runUnitTests gtest.cpp ${test_SRCS})
target_link_libraries(runUnitTests gtest gmock core)
#add_test(runUnitTests runUnitTests) #included in all tutorials but I don't know what it actually does.

test/gtest.cpp

#include "gtest/gtest.h"

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

Note: if you work yourself on a git Project it is better to include the DownloadProject.cmake and DownloadProjects.CmakeLists.cmake.in files inside the project.

Tyndale answered 1/1, 2016 at 17:44 Comment(6)
I had to take out core from test/CMakeLists.txt and re-write src/CMakeLists.txt. After that, it worked!Markitamarkka
@Markitamarkka Thanks.Cuspidation
I followed these instructions as closely as possible, but end up with a load of Error:Configuration Debug The source directory <user Dir>/CLion2016.2/system/cmake/generated/A32N-bdb25b84/bdb25b84/Debug/googletest-src does not contain a CMakeLists.txt file.Astoria
For me it worked changing gmock to gtest_main at test/CMakeLists.txtBurg
@Dormathal. This is a horrible answer leading folks who don't understand CMake to screwup their entire configuration. You're making them rewrite their CMakeLists.txt files without understanding the impact of the changes. A good answer explains what each change does. You could have, for instance, explained why you created additional CMakeList files. How did this get so many upvotes is beyond me!Roseleeroselia
This was awesome! I haven't used CMake in a while, but I'm familiar with its setup, so this was kind of a breeze with your direction. I'm interviewing for a position tomorrow that is primarily for writing gtests and I haven't written gtests in a few years, so this was a great quick setup so that I can freshen my skills before tomorrow. Much thanks and kudos!Eyeball
C
18

1.Git clone the google-test C++ test framework

From https://github.com/google/googletest.git

2.Include the google-test directories

#Add the google test subdirectory
add_subdirectory(PATH_TO_GOOGLETEST)

#include googletest/include dir
include_directories(PATH_TO_GOOGLETEST/googletest/include)

#include the googlemock/include dir
include_directories(PATH_TO_GOOGLETEST/googlemock/include)

3. Link your executable with google-test (This is after creating your executable)

#Define your executable
add_executable(EXECUTABLE_NAME ${SOURCE_FILES})

#Link with GoogleTest
target_link_libraries(EXECUTABLE_NAME gtest gtest_main)

#Link with GoogleMock
target_link_libraries(EXECUTABLE_NAME gmock gmock_main)
Cocker answered 2/7, 2017 at 11:7 Comment(2)
This works perfectly ... Well explained (commented) answerBaguio
This is perfect. Both the google repo and clion gtest documentation does not contain a working example. This works. Thanks a lotKyne
G
3

Here is a small example C++11 project which uses GoogleTest which only relies on packaged CMake features (mainly the ExternalProject module and works both from inside CLion and a *nix command line.

This version shows "vendored" dependencies, which can reside outside of a project if needed. All dependency builds' source code and build artifacts are contained in the project and do not pollute the build host. The ExternalProject module however is fairly easy to adjust to download a specific version off of a remote repo.

Let me know if something needs clarification in README.

Glass answered 11/8, 2016 at 18:6 Comment(2)
Link to sample project is dead.Digitate
Corrected the moved project link. The down vote was not necessary.Glass
U
0

In addition to the @victor-mwenda solution, google-test should be cloned into the project directory.

Unfix answered 8/2 at 10:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.