Skip to content

Commit

Permalink
CMake: Add separate unit test targets
Browse files Browse the repository at this point in the history
Each unit test file ClassName.cpp, which results in the creation
of a test target openshot-ClassName-test, will now also be labeled
with the CTest label ClassName, and a target ClassName_coverage
will be generated that runs only the tests in that file.

This is especially useful when developing tests for a class, as
the tests in the class being worked on can be re-run without
having to wait a minute or more while the other tests run.
  • Loading branch information
ferdnyc committed Apr 19, 2021
1 parent 4e4a95c commit 00caff0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
39 changes: 29 additions & 10 deletions CMakeLists.txt
Expand Up @@ -178,8 +178,9 @@ find_package(Catch2 QUIET)
if(NOT Catch2_FOUND)
set(CAN_BUILD_TESTS FALSE)
endif()
if(CAN_BUILD_TESTS)
enable_testing()
if(CAN_BUILD_TESTS AND BUILD_TESTING)
include(CTest)
include(Catch)
if(ENABLE_PARALLEL_CTEST)
# Figure out the amount of parallelism for CTest
include(ProcessorCount)
Expand All @@ -189,35 +190,53 @@ if(CAN_BUILD_TESTS)
set(CTEST_OPTIONS "-j${CPU_COUNT}")
endif()
endif()
include(CTest)
include(Catch)
add_subdirectory(tests)
endif()
add_feature_info("Unit tests" CAN_BUILD_TESTS "Compile unit tests for library functions")

############## COVERAGE REPORTING #################
if (ENABLE_COVERAGE AND DEFINED UNIT_TEST_TARGETS)
set(COVERAGE_EXCLUDES
"bindings/*"
"examples/*"
"${CMAKE_CURRENT_BINARY_DIR}/bindings/*"
"${CMAKE_CURRENT_BINARY_DIR}/src/*_autogen/*"
)
setup_target_for_coverage_lcov(
NAME coverage
LCOV_ARGS "--no-external"
EXECUTABLE ctest
EXECUTABLE_ARGS ${CTEST_OPTIONS}
DEPENDENCIES openshot ${UNIT_TEST_TARGETS}
EXCLUDE
"bindings/*"
"examples/*"
"${CMAKE_CURRENT_BINARY_DIR}/bindings/*"
"${CMAKE_CURRENT_BINARY_DIR}/src/*_autogen/*"
EXCLUDE ${COVERAGE_EXCLUDES}
)
foreach(_t IN LISTS UNIT_TEST_NAMES)
setup_target_for_coverage_lcov(
NAME "${_t}_coverage"
LCOV_ARGS "--no-external"
EXECUTABLE ctest
EXECUTABLE_ARGS ${CTEST_OPTIONS} -L "^${_t}$"
DEPENDENCIES openshot openshot-${_t}-test
EXCLUDE ${COVERAGE_EXCLUDES}
)
endforeach()
endif()

if(CAN_BUILD_TESTS AND NOT TARGET coverage)
if(DEFINED UNIT_TEST_TARGETS AND NOT TARGET coverage)
add_custom_target(coverage
COMMAND ctest ${CTEST_OPTIONS}
DEPENDS openshot ${UNIT_TEST_TARGETS}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Running unit tests (coverage disabled)"
)
foreach(_t IN LISTS UNIT_TEST_NAMES)
add_custom_target("${_t}_coverage"
COMMAND ctest ${CTEST_OPTIONS} -L "^${_t}$"
DEPENDS openshot openshot-${_t}-test
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Running unit tests for ${_t} class (coverage disabled)"
)
endforeach()
endif()

if(TARGET test AND NOT TARGET os_test)
Expand Down
28 changes: 14 additions & 14 deletions tests/CMakeLists.txt
Expand Up @@ -48,6 +48,8 @@ set(OPENSHOT_TESTS
Color
Coordinate
DummyReader
ReaderBase
ImageWriter
FFmpegReader
FFmpegWriter
Fraction
Expand All @@ -56,20 +58,14 @@ set(OPENSHOT_TESTS
KeyFrame
Point
QtImageReader
ReaderBase
Settings
Timeline
)

# ImageMagick related test files
if(DEFINED CACHE{HAVE_IMAGEMAGICK})
list(APPEND OPENSHOT_TESTS
ImageWriter
)
endif()

# OPENCV RELATED TEST FILES
if(DEFINED CACHE{HAVE_OPENCV})
###
### OPENCV RELATED TEST FILES
###
if(ENABLE_OPENCV)
list(APPEND OPENSHOT_TESTS
CVTracker
CVStabilizer
Expand All @@ -80,7 +76,7 @@ endif()
###
### Catch2 unit tests
###
if (NOT CAN_BUILD_TESTS)
if (NOT CAN_BUILD_TESTS OR NOT BUILD_TESTING)
return()
endif()

Expand All @@ -96,13 +92,17 @@ foreach(tname ${OPENSHOT_TESTS})
target_compile_definitions(openshot-${tname}-test PRIVATE
TEST_MEDIA_PATH="${TEST_MEDIA_PATH}"
)
target_link_libraries(openshot-${tname}-test Catch2::Catch2 openshot)
# Automatically configure CTest targets from Catch2 test cases
catch_discover_tests(
target_link_libraries(openshot-${tname}-test Catch2::Catch2 openshot)
# Automatically configure CTest targets from Catch2 test cases
catch_discover_tests(
openshot-${tname}-test
TEST_PREFIX ${tname}:
PROPERTIES
LABELS ${tname}
)
list(APPEND CATCH2_TEST_TARGETS openshot-${tname}-test)
list(APPEND CATCH2_TEST_NAMES ${tname})
endforeach()
# Export target list for coverage use
set(UNIT_TEST_TARGETS ${CATCH2_TEST_TARGETS} PARENT_SCOPE)
set(UNIT_TEST_NAMES ${CATCH2_TEST_NAMES} PARENT_SCOPE)

0 comments on commit 00caff0

Please sign in to comment.