Use sfml_add_test macro for unit tests and copy dlls to output directory if required

This commit is contained in:
= 2018-11-29 18:38:27 +00:00 committed by Lukas Dürrenberger
parent 2c3a321afd
commit 53972ed5f2
3 changed files with 47 additions and 13 deletions

View File

@ -285,8 +285,12 @@ if(SFML_BUILD_DOC)
add_subdirectory(doc)
endif()
if(SFML_BUILD_TEST_SUITE)
enable_testing()
add_subdirectory(test)
if (SFML_OS_IOS)
message( WARNING "Unit testing not supported on iOS")
else()
enable_testing()
add_subdirectory(test)
endif()
endif()
# on Linux and BSD-like OS, install pkg-config files by default

View File

@ -297,6 +297,42 @@ macro(sfml_add_example target)
endmacro()
# add a new target which is a SFML test
# example: sfml_add_test(sfml-test
# ftp.cpp ...
# sfml-network)
function(sfml_add_test target SOURCES DEPENDS)
# set a source group for the source files
source_group("" FILES ${SOURCES})
# check whether resources must be added in target
set(target_input ${SOURCES})
# create the target
add_executable(${target} ${target_input})
# set the target's folder (for IDEs that support it, e.g. Visual Studio)
set_target_properties(${target} PROPERTIES FOLDER "Tests")
# link the target to its SFML dependencies
if(DEPENDS)
target_link_libraries(${target} PRIVATE ${DEPENDS})
endif()
# Add the test
add_test(${target} ${target})
# If building shared libs on windows we must copy the dependencies into the folder
if (WIN32 AND BUILD_SHARED_LIBS)
foreach (DEPENDENCY ${DEPENDS})
add_custom_command(TARGET ${target} PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_FILE:${DEPENDENCY}>
$<TARGET_FILE_DIR:${target}>)
endforeach()
endif()
endfunction()
# Create an interface library for an external dependency. This virtual target can provide
# link specifications and include directories to be used by dependees.

View File

@ -12,9 +12,7 @@ SET(SYSTEM_SRC
"${SRCROOT}/TestUtilities/SystemUtil.hpp"
"${SRCROOT}/TestUtilities/SystemUtil.cpp"
)
add_executable(systemtest ${SYSTEM_SRC})
target_link_libraries(systemtest sfml-system)
add_test(System-module-tests systemtest)
sfml_add_test(systemtest "${SYSTEM_SRC}" sfml-system)
if(SFML_BUILD_WINDOW)
SET(WINDOW_SRC
@ -22,9 +20,7 @@ if(SFML_BUILD_WINDOW)
"${SRCROOT}/TestUtilities/WindowUtil.hpp"
"${SRCROOT}/TestUtilities/WindowUtil.cpp"
)
add_executable(windowtest ${WINDOW_SRC})
target_link_libraries(windowtest sfml-window)
add_test(Window-module-tests windowtest)
sfml_add_test(windowtest "${WINDOW_SRC}" sfml-window)
endif()
if(SFML_BUILD_GRAPHICS)
@ -34,17 +30,15 @@ if(SFML_BUILD_GRAPHICS)
"${SRCROOT}/TestUtilities/GraphicsUtil.hpp"
"${SRCROOT}/TestUtilities/GraphicsUtil.cpp"
)
add_executable(graphicstest ${GRAPHICS_SRC})
target_link_libraries(graphicstest sfml-graphics)
add_test(Graphics-module-tests graphicstest)
sfml_add_test(graphicstest "${GRAPHICS_SRC}" sfml-graphics)
endif()
# Automatically run the tests at the end of the build
add_custom_target(runtests ALL
DEPENDS systemtest windowtest graphicstest
)
add_custom_command(TARGET runtests
COMMENT "Run tests"
POST_BUILD COMMAND ctest ARGS --output-on-failure
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
)