Search code examples
qtcmakegoogletestqmake

How do I integrate a local Qt project with Google Test?


I'm trying to integrate Google Test into my Qt project. I have basic (EXPECT_EQ(1, 1)) tests running with Google Test, but am confused how I can integrate my Qt project (which uses qmake) into the test's CMake build.

Google Test is currently added using the recommended CMakeLists.txt from their README. I would like to add my local src and build to the test build. My src is located (relative to the test folder) ../src/ and I've built the src into (relative to the test folder) build/project-build/ but I believe it won't integrate because the project has .pro's. The error when I try to integrate it as a subdirectory:

CMake Error at CMakeLists.txt:31 (add_subdirectory):
  The source directory

    /home/project/src

  does not contain a CMakeLists.txt file.

This is how I am adding it as a subdirectory and project to my CMakeLists.txt:

add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/../../src
                 ${CMAKE_CURRENT_BINARY_DIR}/project-build
                 EXCLUDE_FROM_ALL)

In a separate CMakeLists.txt.in (similar to how google test does it):

include(ExternalProject)
ExternalProject_Add(project
  GIT_REPOSITORY    "${CMAKE_CURRENT_BINARY_DIR}/../.."
  GIT_TAG           master
  SOURCE_DIR        "${CMAKE_CURRENT_BINARY_DIR}/../../src"
  BINARY_DIR        "${CMAKE_CURRENT_BINARY_DIR}/project-build"
  CONFIGURE_COMMAND ""
  BUILD_COMMAND     ""
  INSTALL_COMMAND   ""
  TEST_COMMAND      ""
)

Also, please let me know if this is a difficult way to test a project. I was hoping to use google test, but if this doesn't work will probably use Qt Test. I am fairly new to C++ and new to C++ unit testing (am familiar with Java and JUnit). Would appreciate any help, thank you!


Solution

  • When using add_subdirectory(), you must have a CMakeLists.txt file in the added source sub-directory (the first argument to the call). This directory typically contains the code files to be built, but they will be built by CMake, not qmake. Because you are trying to mix two build approaches, there are a few options to try to simplify things:

    1. You can first build the Google Test framework with CMake, then pull the relevant GTest includes/libs into your local src build with qmake afterwards. There is a decent example here documenting some of the steps in more detail.
    2. Depending on the size/scope of your local src, you could convert your Qt project to build with cmake, instead of qmake. There are some helpful answers here and here to demonstrate how convert qmake projects to CMake projects. This way, CMake can handle the Google Test and local src builds.
    3. Take your suggested approach, and use Qt's built in testing framework, instead of the Google Test framework.