Add Catch2 as C++ test framework

Use -DCMAKE_BUILD_TEST_SUITE=1 during CMake invocation to activate building the tests.
The 'tests' target builds the unit tests which can then be executed using the 'test' target.

We depend on the Catch2 version of the system.
Since development normally takes place on current OSs, this shouldn't pose a problem.
This commit is contained in:
Peter Keresztes Schmidt 2021-02-02 22:57:50 +01:00
parent 2f12f82e82
commit bef4e4032f
4 changed files with 65 additions and 1 deletions

View File

@ -134,6 +134,7 @@ mark_as_advanced(
ZM_SYSTEMD
ZM_MANPAGE_DEST_PREFIX)
option(BUILD_TEST_SUITE "Build the test suite" 0)
option(BUILD_MAN "Build man pages" 1)
set(ZM_RUNDIR "/var/run/zm" CACHE PATH
@ -899,6 +900,16 @@ add_subdirectory(web)
add_subdirectory(misc)
add_subdirectory(onvif)
if(BUILD_TEST_SUITE)
message("Building unit tests: Yes")
find_package(Catch2 REQUIRED)
include(CTest)
add_subdirectory(tests)
else()
message("Building unit tests: No (default)")
endif(BUILD_TEST_SUITE)
# Process distro subdirectories
if((ZM_TARGET_DISTRO MATCHES "^el") OR (ZM_TARGET_DISTRO MATCHES "^fc"))
add_subdirectory(distros/redhat)

View File

@ -72,6 +72,11 @@ set(ZM_BIN_SRC_FILES
# A fix for cmake recompiling the source files for every target.
add_library(zm STATIC ${ZM_BIN_SRC_FILES})
target_include_directories(zm
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR})
link_directories(libbcrypt)
add_executable(zmc zmc.cpp)

29
tests/CMakeLists.txt Normal file
View File

@ -0,0 +1,29 @@
# This file is part of the ZoneMinder Project.
#
# This file is free software; as a special exception the author gives
# unlimited permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
include(Catch)
set(TEST_SOURCES)
add_executable(tests main.cpp ${TEST_SOURCES})
target_link_libraries(tests
PRIVATE
zm
${ZM_BIN_LIBS}
bcrypt
Catch2::Catch2)
target_include_directories(tests
PRIVATE
${CMAKE_CURRENT_BINARY_DIR})
catch_discover_tests(tests)

19
tests/main.cpp Normal file
View File

@ -0,0 +1,19 @@
/*
* This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define CATCH_CONFIG_MAIN
#include "catch2/catch.hpp"