I'm having trouble integrating googletest into my existing project. I put together a simple project to represent my project's structure:
Project Structure
CMakeLists.txt:
cmake_minimum_required(VERSION 3.13)
project(TestTester)
set(CMAKE_CXX_STANDARD 14)
include_directories(existing_source)
add_subdirectory(existing_source)
add_subdirectory(new_test_source)
existing_source/CMakeLists.txt:
cmake_minimum_required(VERSION 3.13)
project(Test_TestTester)
set(CMAKE_CXX_STANDARD 14)
add_executable(TestTester main.cpp existing.h)
new_test_source/CMakeLists.txt:
cmake_minimum_required(VERSION 3.13)
project(Test_TestTester)
set(CMAKE_CXX_STANDARD 14)
find_package(PkgConfig REQUIRED)
pkg_check_modules(gtest REQUIRED gtest>=1.8.1)
SET(CMAKE_CXX_FLAGS -pthread)
enable_testing()
include_directories(${gtest_INCLUDE_DIRS})
add_executable(Test_TestTester main_test.cpp ../existing_source/existing.h)
target_link_libraries(Test_TestTester ${gtest_LIBRARIES})
add_test(NAME Test COMMAND Test_TestTester)
existing_source/existing.h
#ifndef TESTTESTER_EXISTING_H
#define TESTTESTER_EXISTING_H
int sample() {
return 1;
}
#endif //TESTTESTER_EXISTING_H
existing_source/main.cpp
#include <iostream>
#include "existing.h"
int main() {
std::cout << "sample() output = " << sample() << std::endl;
return 0;
}
new_test_source/main_test.cpp
#include <gtest/gtest.h>
#include "../existing_source/existing.h"
TEST(SampleTestCase, TestOneIsOne) {
EXPECT_EQ(1, 1);
}
TEST(ExistingCodeTestCase, TestSample) {
EXPECT_EQ(1, sample());
}
GTEST_API_ int main(int argc, char **argv) {
printf("Running main() from %s\n", __FILE__);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Goal:
Building with CMake will create two executables, one, TestTester, and another called Test_TestTester (sorry for the odd name, it looks like I could have chosen a better project name!).
TestTester will be the main project executable which will run the code from existing_course/main.cpp and output sample() output = 1
.
Test_TestTester should be the unit tests from main_test.cpp which tests that 1 == 1
and 1 == sample()
. This should run when the project is built.
Attempts:
I've tried using CMake's add_subdirectory() to expose a second CMakeLists.txt in the test subdirectory which has its own add_executable() with the name of the test program, however I cannot find any output related to the test program. Using enable_testing() followed by add_test() is also failing to produce any changes.
Update:
I realized some problems and assumptions were wrong.
- Within CLion, it defaults to building a particular target. Build all (
cmake --build ... --target all
) must be invoked to build the other executables. - The other questions I read related to this don't seem to be using the pre-compiled library. I compiled and installed googletest on my machine prior to its inclusion into the project.
- This may not be a problem, but it looks like most people choose to structure their projects with each directory having its own CMakeLists.txt file. I reorganized mine to match to make following others' advice easier.
I updated the CMakeLists files with my changes. Using --target all
builds everything appropriately, but I still can't get the tests to run when the project is built.
get the tests to run
what do you mean? How? Is there an error message? What did you tried? How do you run them? Does the test executable build properly? What if you run the test executable by itself? And why do you provide the definition for a symbol with external linkage inside a .h file? Shouldn't thesample
function havestatic
orstatic inline
at least? – Franni