Help Regarding googletest and how to integrate it with the CI pipeline

Hi everyone, I am looking for some help on how to properly set up googletest into a C++ CI pipeline.

While this isn’t my first time using GitLab, it is my first time trying to set up a C++ project. I’ve been using GitLab for C# projects and didn’t have a lot of trouble setting up NUnit to test my projects in the pipeline. I am having difficulty with setting up googletest and am having trouble finding help using Google and the GitLab documentation. The only documentation I have found regarding googletest is setting up reports with test results which will be extremely useful once I have testing done.

Note before I go further: I’m hosting my own GitLab CE instance on my own server. GitLab version 13.7.0 (91da1dcc7a1).

I have a simple C++ project with just one test right now. I found some other examples like the one here: GitHub - pothitos/gtest-demo-gitlab: Unit test demo using Google Test and GitLab CI but doesn’t really go into details on what does what and why things are working. I can get googletest to initiate correctly in the pipeline and I believe I am getting certain things to build but ran into an error I can’t figure out how to resolve:

make[2]: *** No rule to make target ‘…/external/googletest/googletest/src/gtest-all.cc’, needed by ‘CMakeFiles/googletest.dir/external/googletest/googletest/src/gtest-all.cc.o’. Stop.

42make[1]: *** [CMakeFiles/Makefile2:594: CMakeFiles/googletest.dir/all] Error 2

Any help will be appreciated.

.gitlab-ci.yml

image: gcc

stages:

  • build
  • test

project-build:
stage: build
before_script:
- mkdir -p $HOME/.local
- curl -s “https://cmake.org/files/v3.15/cmake-3.15.0-Linux-x86_64.tar.gz” | tar --strip-components=1 -xz -C $HOME/.local
- export PATH=$HOME/.local/bin:$PATH

script:
- ls -l ./googletest
- cmake -S . -B build
- cmake --build build
- ls -l
- ls -l build
artifacts:
paths:
- build/test/test-exe
expire_in: 1 week

project-test:
stage: test
script:
- gtest.exe --gtest_output=“xml:report.xml”
artifacts:
when: always
reports:
junit: report.xml

CMakeLists.txt

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

project(gtest-demo)

enable_language©
enable_language(CXX)

if(CMAKE_CXX_COMPILER_ID MATCHES GNU)
set(CMAKE_CXX_FLAGS “-Wall -Wno-unknown-pragmas -Wno-sign-compare -Woverloaded-virtual -Wwrite-strings -Wno-unused”)
set(CMAKE_CXX_FLAGS_DEBUG “-O0 -g3”)
set(CMAKE_CXX_FLAGS_RELEASE “-O3”)
set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage”)
endif()

include_directories(
${PROJECT_SOURCE_DIR}/src
)

add_library(
pch
src/Sample-Test1/Sample-Test1/pch.cpp
)

set(GOOGLETEST_ROOT external/googletest/googletest CACHE STRING “Google Test source root”)

include_directories(
{PROJECT_SOURCE_DIR}/{GOOGLETEST_ROOT}
{PROJECT_SOURCE_DIR}/{GOOGLETEST_ROOT}/include
)

set(GOOGLETEST_SOURCES
{PROJECT_SOURCE_DIR}/{GOOGLETEST_ROOT}/src/gtest-all.cc
{PROJECT_SOURCE_DIR}/{GOOGLETEST_ROOT}/src/gtest_main.cc
)

foreach(_source {GOOGLETEST_SOURCES}) set_source_files_properties({_source} PROPERTIES GENERATED 1)
endforeach()

add_library(googletest ${GOOGLETEST_SOURCES})

add_executable(
unit_tests
googletest
)

add_dependencies(unit_tests googletest)

target_link_libraries(
unit_tests
googletest
example
pthread
)

include(CTest)
enable_testing()

add_test(unit ${PROJECT_BINARY_DIR}/unit_tests)

Have you eliminated simple path problems?

Your CMake configuration establishes the googletest sources to lie under a path ./external/googletest/googletest/ - does this path exist in the exact structure?

I ask this because your CI job script is performing a ls -l ./googletest in the project source directory which does not appear to be under external.

Thanks for responding!

I noticed that yesterday, but after changing the directory I was still getting the same error. I kept searching for other solutions and I was able to find this:

Using this I was able to get it to build successfully but going to try and get the pipeline to actually build the tests today.

Is there a plan to add direct GitLab documentation for googletest in the future?