cmake Einbinden von GTest

Letzte Änderung: 29.01.2017

GTest (Google Test) ist ein einfach anzuwendentes Framework für Unit Tests.

Im Folgenden wird gezeigt, wie es mit cmake in ein Projekt eingebunden wird.

Pro Klasse wird eine Testsklasse werden in einer cpp-Datei angelegt. Das Namensschema ist dabei: KLASSE_test.h, KLASSE_test.cpp

ttnlogic_test.cpp, ttnlogic_test.h
ttnpath_test.cpp, ttnpath_test.h
ttnstring_test.cpp, ttnstring_test.h

Mit folgender for-Schleife werden die Tests erzeugt:

#
# Copyright 2016-2017 Thomas Thielemann info@th-thielemann.de
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# cmake file for tests
############################################################
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)

############################################################
# GTest integration
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

# Search path
include_directories("${PROJECT_SOURCE_DIR}/src")
include_directories("/usr/include")

# Additional sources for all targets
set(SOURCES
    ${SOURCES} # library sources
    # add sources here
)

# Additional Libraries
set(TESTLIBS ${EXTRA_LIBS}
    # add all required libs here
)

# Generates a separate test application and test for each element
# Tests have to be in a source file testMYTESTCLASS.cpp
# The resulting test application is named MYTESTCLASS
foreach(TESTCLASS
        ttnlogic
        ttnpath
        ttnstring
)
    add_executable(test${TESTCLASS} test${TESTCLASS}.cpp ${SOURCES})
    target_link_libraries(test${TESTCLASS} GTest::GTest GTest::Main ${TESTLIBS})
    add_test(test${TESTCLASS} test${TESTCLASS})
    install(TARGETS test${TESTCLASS} RUNTIME DESTINATION ${PROJECT_BINARY_DIR}/bin )
endforeach(TESTCLASS)




© 2003-2017 th-thielemann.de