Home > Software engineering >  Running googletest with separated src- and test-folders
Running googletest with separated src- and test-folders

Time:10-14

Here my minimal example (https://github.com/theCollectiv/cmake_minimal). I first got this project structure with seperate Headerplacement (-> src and include)

.
├── 1Cmake.sh
├── 2runExe.sh
├── 3cleanTarget.sh
├── 4runTest.sh
├── 7VersionOfCurrentTools.sh
├── build
│   ├── CMakeCache.txt
│   ├──...
│   ...
├── CMakeDefaults.txt
├── CMakeLists.txt
├── compile_commands.json -> ...
├── data
├── docs
│   ├── ...
├── include
│   └── addition
│       └── addition.hpp
├── src
│   ├── addition
│   │   └── addition.cpp
│   ├── main.cpp
├── tests
│   └── test01_proofOfWork
│       ├── CMakeLists.txt
│       └── tests.cpp

main.cpp

#include "addition.hpp"

#include <iostream>
int main() {
  int a = 5;
  int b = 3;
  std::cout << "a is " << a << "\nb is " << b << "\nThe Sum of both "
<< add(a, b) << std::endl; }

addition.cpp

#include "addition.hpp"
int add(int a, int b) { return a   b; }

header of addition.cpp: addition.hpp

#pragma once
int add(int a, int b);

The CMakeList.txt in the root-folder looks like this:

cmake_minimum_required(VERSION 3.21.2)
set (This
    Project_main)

project(${This}
    LANGUAGES CXX
    VERSION 1.000)

enable_testing()
find_package(GTest REQUIRED)

add_subdirectory(./tests/test01_proofOfWork)

# Custom Variables
set(QUELLE src/main.cpp)
set(ZIEL finalExecutable)

# integrate lib (with .cpp and its header)
set(LIB_1 addition)

add_library(                                
    ${LIB_1}                                
    STATIC src/addition/addition.cpp        
    )
# add .hpp hinzufuegen and connect to lib
target_include_directories(              
    ${LIB_1}                             
    PUBLIC include/addition             
    )
# add executable
add_executable(${ZIEL} ${QUELLE})       

# linking the lib  
target_link_libraries(                  
    ${ZIEL}                             
    PRIVATE ${LIB_1}                    
    #    PUBLIC ${LIB_1}                    
    )

Now I want to add unit tests by using googletest. I'm using ubuntu and googletest is installed in my systempath (with shared libs) and working.

I'm now a little bit stuck, getting googletest well integrated in the project.

The CMakeList.txt in the tests-dir:

# Name of the Tests
set (This
    runTest)

# Location of the test
set (Sources
    tests.cpp)

# executable of the test
add_executable(${This} ${Sources})

# linking libs for the test
target_link_libraries(${This} PUBLIC
    GTest::gmock
    GTest::gtest
    GTest::gmock_main
    GTest::gtest_main)
# registrating the test 
gtest_discover_tests(${This})

The test.cpp

// tests.cpp
// #include "./../../include/addition/addition.hpp"
#include <gtest/gtest.h>
#include <gtest/internal/gtest-internal.h>

// bool foo(){
//     return true;
// }
// 
// 
// TEST(Simpletest, trueEqualsTrue) {
//     EXPECT_TRUE(foo());
//     EXPECT_FALSE(foo());
//     EXPECT_EQ(true, foo());
//     ASSERT_FALSE(foo());
//     EXPECT_EQ(false, foo());
// }
//
TEST(SquareRootTest, PositiveNos) {
    ASSERT_EQ(18.0, add(1,17));
    EXPECT_EQ(6, add(1,6));
    ASSERT_EQ(25.4, add(55, -1));
    ASSERT_EQ(0, add(-4, 4));
}


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

The de-commented test code works and compiles fine, so gtest works fine (means: if I m testing code, thats in the same file the code is tested).

But... (I'm new to this stuff, so maybe its an easy (or noob) question): The reason, why I added the googletest is to test the code in the src- or -include dir. Therefore I got the add all the headers and files in the these dirs for test cases all the time. Is there a good way to make all of them available for the possible tests that I might run without configuring it all the time?


My problem is,

  1. that the de-commented code works/compiles fine (of code in the same location like the test is). That means:
    • googletest is installed correctly
    • the tests-CMakeLists.txt is working correctly (and the root-CmakeLists.txt too)
  2. If I want to compile the test-code, that is not de-commented (that means the 'code to test' is not in the same file like the test itself), it is complaining for missing headers. If I add them, the compilation of the tests quits with errors (like "cmake line XXX" ...doesnt tell me anything or "undefined reference too"). The problem I got
    • it doesnt compile the tests (obvious)
    • even if it would compile, I got to rebuild all the things (adding headers in the test-source-code) in the test-directory like I did in the root-directory (similar/same file linking in both of them). If I got a more complex project structure (using a function in a file which uses functions from another file), this is 'doing the same exact same stuff in for the normal project (src and include) and in the tests-dir'. Or am I wrong at this point?

Solution:

# linking libs for the test
target_link_libraries(${This} PUBLIC
    GTest::gmock
    GTest::gtest
    GTest::gmock_main
    GTest::gtest_main
    # Solution: Got to link the lib(s) (which I want to test in the root-dir) against the test-executable
    addition
    )

CodePudding user response:

You are not linking yout test project against your library. So it doesn't use your library. Link it.

target_link_libraries(${This} addition)
  • Related