Skip to content
Snippets Groups Projects
Commit d97ab5c7 authored by Tomáš Pecka's avatar Tomáš Pecka Committed by Jan Trávníček
Browse files

CMake: support for integration tests with Catch

parent cf6e0851
No related branches found
No related tags found
No related merge requests found
find_package(Catch2 REQUIRED)
include(Catch)
project({project_name})
set(PROJECT_NAME_TEST test-{project_name})
{cmake_options}
{find_packages_tests}
set(SOURCE_FILES_TEST
{source_files_test}
)
set( TEST_FILES_BASEDIR ${{CMAKE_SOURCE_DIR}}/examples2 )
configure_file (
${{CMAKE_SOURCE_DIR}}/CMake/testfiles.hpp.in
${{CMAKE_BINARY_DIR}}/generated/testfiles_basedir.hpp
)
add_executable(${{PROJECT_NAME_TEST}} ${{SOURCE_FILES_TEST}})
set_target_properties(${{PROJECT_NAME_TEST}} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
)
target_link_libraries(${{PROJECT_NAME_TEST}} {target_test_libs} Catch2::Catch2)
# Include dependencies directories
target_include_directories(${{PROJECT_NAME_TEST}}
PUBLIC ${{CMAKE_CURRENT_SOURCE_DIR}}/test-src # anything that depends on this should include src also, hence public
${{CMAKE_BINARY_DIR}}/generated # testfiles header
{include_test_paths}
)
# if (CMAKE_BUILD_TYPE STREQUAL "Release")
# set_property ( TARGET ${{PROJECT_NAME_TEST}} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE )
# endif ()
catch_discover_tests(
${{PROJECT_NAME_TEST}}
TEST_SPEC "[integration]"
TEST_PREFIX "[integration] "
)
...@@ -91,6 +91,10 @@ set(ALIB_MODULES_LIB ...@@ -91,6 +91,10 @@ set(ALIB_MODULES_LIB
set(ALIB_MODULES_EXE set(ALIB_MODULES_EXE
{alib_modules_exe} {alib_modules_exe}
) )
set(ALIB_MODULES_TEST
{alib_modules_test}
)
   
################## ##################
# configure version # configure version
...@@ -105,15 +109,6 @@ add_subdirectory ( contrib/catch2 ) ...@@ -105,15 +109,6 @@ add_subdirectory ( contrib/catch2 )
   
################## ##################
# Register modules # Register modules
foreach (module ${{ALIB_MODULES_LIB}} ${{ALIB_MODULES_EXE}}) foreach (module ${{ALIB_MODULES_LIB}} ${{ALIB_MODULES_EXE}} ${{ALIB_MODULES_TEST}})
add_subdirectory(${{module}}) add_subdirectory(${{module}})
endforeach () endforeach ()
#######
# Tests
# - enable
enable_testing()
# include(CTest) # we do not need full CTest support now
# register bash tests
# TODO
...@@ -18,6 +18,7 @@ gui ...@@ -18,6 +18,7 @@ gui
root: CMakeLists_root.txt root: CMakeLists_root.txt
executable: CMakeLists_bin.txt executable: CMakeLists_bin.txt
library: CMakeLists_lib.txt library: CMakeLists_lib.txt
testing: CMakeLists_itest.txt
   
[CMake:Sources] [CMake:Sources]
; where to find sources, relative to {PROJECT_DIR} ; where to find sources, relative to {PROJECT_DIR}
......
...@@ -205,6 +205,7 @@ class Generator: ...@@ -205,6 +205,7 @@ class Generator:
f.write(CATEGORIES['root'].format( f.write(CATEGORIES['root'].format(
alib_modules_lib=Helpers.join([p.name for p in projects if p.category == "library"]), alib_modules_lib=Helpers.join([p.name for p in projects if p.category == "library"]),
alib_modules_exe=Helpers.join([p.name for p in projects if p.category == "executable"]), alib_modules_exe=Helpers.join([p.name for p in projects if p.category == "executable"]),
alib_modules_test=Helpers.join([p.name for p in projects if p.category == "testing"]),
alib_versioning_major=Config.get(CONFIG, 'Versioning', 'major'), alib_versioning_major=Config.get(CONFIG, 'Versioning', 'major'),
alib_versioning_minor=Config.get(CONFIG, 'Versioning', 'minor'), alib_versioning_minor=Config.get(CONFIG, 'Versioning', 'minor'),
alib_versioning_patch=Config.get(CONFIG, 'Versioning', 'patch'), alib_versioning_patch=Config.get(CONFIG, 'Versioning', 'patch'),
...@@ -230,6 +231,20 @@ class Generator: ...@@ -230,6 +231,20 @@ class Generator:
find_packages_tests=Helpers.join(map(lambda p: "find_package({})".format(p), test_finds), "\n"), find_packages_tests=Helpers.join(map(lambda p: "find_package({})".format(p), test_finds), "\n"),
)) ))
   
@classmethod
def generate_testing(cls, project, dry_run):
test_sys_includes, test_sys_libs, test_finds = cls._handle_system_deps(project, project.system_deps_test)
with SmartOpen("CMakeLists.txt", 'w', directory=project.path, dry_run=dry_run) as f:
f.write(CATEGORIES[project.category].format(
project_name=project.name,
target_test_libs=Helpers.join(project.dependencies_test + test_sys_libs, ' '),
include_test_paths=Helpers.join(test_sys_includes, ' '),
cmake_options=Helpers.join(["set({} {})".format(k.upper(), v) for k, v in project.cmake_additional_set], sep='\n'),
source_files_test=Helpers.join(project.find_sources_test()),
find_packages_tests=Helpers.join(map(lambda p: "find_package({})".format(p), test_finds), "\n"),
))
@classmethod @classmethod
def generate_executable(cls, project, dry_run): def generate_executable(cls, project, dry_run):
sys_includes, sys_libs, finds = cls._handle_system_deps(project, project.system_deps) sys_includes, sys_libs, finds = cls._handle_system_deps(project, project.system_deps)
...@@ -376,7 +391,7 @@ if __name__ == '__main__': ...@@ -376,7 +391,7 @@ if __name__ == '__main__':
   
if args.version is True: if args.version is True:
print("{}.{}.{}".format(Config.get(CONFIG, 'Versioning', 'major'), print("{}.{}.{}".format(Config.get(CONFIG, 'Versioning', 'major'),
Config.get(CONFIG, 'Versioning', 'minor'), Config.get(CONFIG, 'Versioning', 'minor'),
Config.get(CONFIG, 'Versioning', 'patch'))) Config.get(CONFIG, 'Versioning', 'patch')))
sys.exit(0) sys.exit(0)
   
......
#ifndef __TEST_FILES_HPP_
#define __TEST_FILES_HPP_
#define TEST_FILES_BASEDIR "${TEST_FILES_BASEDIR}"
#endif /* __TEST_FILES_HPP_ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment