Skip to content

Commit 8cefd49

Browse files
authored
Merge pull request #635 from OpenShot/catch-tests
Switch unit tests to Catch2 framework
2 parents 982558e + 2255852 commit 8cefd49

30 files changed

+2431
-2398
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ jobs:
3535
libopenshot-audio-dev \
3636
qtbase5-dev qtbase5-dev-tools \
3737
libfdk-aac-dev libavcodec-dev libavformat-dev libavdevice-dev libavutil-dev libavfilter-dev libswscale-dev libpostproc-dev libswresample-dev \
38-
libzmq3-dev libmagick++-dev libunittest++-dev \
38+
libzmq3-dev libmagick++-dev \
3939
libopencv-dev libprotobuf-dev protobuf-compiler
40+
# Install catch2 package from Ubuntu 20.10, since for some reason
41+
# even 20.04 only has Catch 1.12.1 available.
42+
wget https://launchpad.net/ubuntu/+archive/primary/+files/catch2_2.13.0-1_all.deb
43+
sudo dpkg -i catch2_2.13.0-1_all.deb
44+
4045
4146
- name: Build libopenshot
4247
shell: bash
@@ -51,7 +56,7 @@ jobs:
5156
shell: bash
5257
run: |
5358
pushd build
54-
cmake --build . --target os_test -- VERBOSE=1
59+
cmake --build . --target coverage -- VERBOSE=1
5560
popd
5661
5762
- name: Install libopenshot
@@ -65,4 +70,3 @@ jobs:
6570
if: ${{ matrix.compiler == 'clang' }}
6671
with:
6772
file: build/coverage.info
68-

CMakeLists.txt

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,15 @@ include(FeatureSummary)
7373
# Optional build settings for libopenshot
7474
option(USE_SYSTEM_JSONCPP "Use system installed JsonCpp, if found" ON)
7575
option(DISABLE_BUNDLED_JSONCPP "Don't fall back to bundled JsonCpp" OFF)
76+
7677
option(ENABLE_IWYU "Enable 'Include What You Use' scanner (CMake 3.3+)" OFF)
77-
option(ENABLE_TESTS "Build unit tests (requires UnitTest++)" ON)
78+
79+
option(ENABLE_TESTS "Build unit tests (requires Catch2)" ON)
80+
option(ENABLE_PARALLEL_CTEST "Run CTest using multiple processors" ON)
7881
option(ENABLE_COVERAGE "Scan test coverage using gcov and report" OFF)
82+
7983
option(ENABLE_DOCS "Build API documentation (requires Doxygen)" ON)
84+
8085
option(APPIMAGE_BUILD "Build to install in an AppImage (Linux only)" OFF)
8186
option(ENABLE_MAGICK "Use ImageMagick, if available" ON)
8287
option(ENABLE_OPENCV "Build with OpenCV algorithms (requires Boost, Protobuf 3)" ON)
@@ -87,7 +92,7 @@ if (DISABLE_TESTS)
8792
endif()
8893

8994
if(DEFINED ENABLE_TESTS)
90-
set(ENABLE_TESTS ${ENABLE_TESTS} CACHE BOOL "Build unit tests (requires UnitTest++)" FORCE)
95+
set(ENABLE_TESTS ${ENABLE_TESTS} CACHE BOOL "Build unit tests (requires Catch2)" FORCE)
9196
endif()
9297

9398
#### Work around a GCC < 9 bug with handling of _Pragma() in macros
@@ -109,7 +114,7 @@ ENDIF(WIN32)
109114
############## Code Coverage #########################
110115
if (ENABLE_COVERAGE AND NOT ENABLE_TESTS)
111116
message(WARNING "ENABLE_COVERAGE requires unit tests, forcing ENABLE_TESTS")
112-
set(ENABLE_TESTS ON CACHE BOOL "Don't build unit tests" FORCE)
117+
set(ENABLE_TESTS ON CACHE BOOL "Build unit tests (requires Catch2 or UnitTest++)" FORCE)
113118
endif()
114119

115120
if (ENABLE_COVERAGE)
@@ -166,55 +171,87 @@ if (ENABLE_DOCS)
166171
OPTIONAL ) # No error if the docs aren't found
167172
endif()
168173
endif()
169-
add_feature_info("Documentation" DOCS_ENABLED "Build API documentation with 'make doc'")
170174

171175
############# PROCESS tests/ DIRECTORY ##############
172176
if(ENABLE_TESTS)
173177
set(TESTS_ENABLED TRUE) # May be overridden by tests/CMakeLists.txt
178+
find_package(Catch2 REQUIRED)
179+
if(ENABLE_PARALLEL_CTEST)
180+
# Figure out the amount of parallelism for CTest
181+
include(ProcessorCount)
182+
ProcessorCount(CPU_COUNT)
183+
if(NOT CPU_COUNT EQUAL 0)
184+
message(STATUS "Setting up unit tests to use ${CPU_COUNT} processors")
185+
set(CTEST_OPTIONS "-j${CPU_COUNT}")
186+
endif()
187+
endif()
188+
include(CTest)
189+
include(Catch)
174190
add_subdirectory(tests)
175191
endif()
176192
add_feature_info("Unit tests" TESTS_ENABLED "Compile unit tests for library functions")
177193

178194
############## COVERAGE REPORTING #################
179-
if (ENABLE_COVERAGE)
195+
if (ENABLE_COVERAGE AND DEFINED UNIT_TEST_TARGETS)
180196
setup_target_for_coverage_lcov(
181197
NAME coverage
182198
LCOV_ARGS "--no-external"
183-
EXECUTABLE openshot-test
184-
DEPENDENCIES openshot openshot-test
199+
EXECUTABLE ctest
200+
EXECUTABLE_ARGS ${CTEST_OPTIONS}
201+
DEPENDENCIES openshot ${UNIT_TEST_TARGETS}
185202
EXCLUDE
186203
"bindings/*"
187204
"examples/*"
188205
"${CMAKE_CURRENT_BINARY_DIR}/bindings/*"
189206
"${CMAKE_CURRENT_BINARY_DIR}/src/*_autogen/*"
190207
)
191-
if(NOT TARGET os_test)
192-
add_custom_target(os_test)
193-
add_dependencies(os_test coverage)
194-
endif()
195208
endif()
196209

197-
# Also hook up 'test' as an alias for the 'os_test' target, if possible
198-
# This requires CMake 3.11+, where the CMP0037 policy
199-
# configured to 'NEW' mode will not reserve target names
200-
# unless the corresponding feature is actually used
201-
if (POLICY CMP0037)
202-
cmake_policy(SET CMP0037 NEW)
210+
if(TESTS_ENABLED AND NOT TARGET coverage)
211+
add_custom_target(coverage
212+
COMMAND ctest ${CTEST_OPTIONS}
213+
DEPENDS openshot ${UNIT_TEST_TARGETS}
214+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
215+
COMMENT "Running unit tests (coverage disabled)"
216+
)
203217
endif()
204-
if(TARGET os_test)
205-
if (CMAKE_VERSION VERSION_GREATER 3.11)
206-
message(STATUS "Cmake 3.11+ detected, enabling 'test' target")
207-
add_custom_target(test)
208-
add_dependencies(test os_test)
209-
set(TEST_TARGET_NAME "test")
210-
else()
211-
set(TEST_TARGET_NAME "os_test")
218+
219+
if(TARGET test AND NOT TARGET os_test)
220+
add_custom_target(os_test)
221+
add_dependencies(os_test coverage)
222+
endif()
223+
224+
if(TARGET os_test AND NOT TARGET test AND CMAKE_VERSION VERSION_GREATER 3.11)
225+
# Also hook up 'test' as an alias for the 'os_test' target, if possible
226+
# This requires CMake 3.11+, where the CMP0037 policy
227+
# configured to 'NEW' mode will not reserve target names
228+
# unless the corresponding feature is actually used
229+
if (POLICY CMP0037)
230+
cmake_policy(SET CMP0037 NEW)
212231
endif()
213-
add_feature_info("Testrunner" ENABLE_TESTS "Run unit tests with 'make ${TEST_TARGET_NAME}'")
232+
message(STATUS "Cmake 3.11+ detected, enabling 'test' target")
233+
add_custom_target(test)
234+
add_dependencies(test os_test)
214235
endif()
215236

237+
###
238+
### Add feature-summary details on non-default built targets
239+
###
240+
set(optional_targets test os_test coverage doc)
241+
set(target_test_description "Build and execute unit tests")
242+
set(target_os_test_description "Build and execute unit tests (legacy target)")
243+
set(target_coverage_description "Run unit tests and (if enabled) collect coverage data")
244+
set(target_doc_description "Build formatted API documentation (HTML+SVG)")
245+
foreach(_tname IN LISTS optional_targets)
246+
if(TARGET ${_tname})
247+
add_feature_info("Non-default target '${_tname}'" TRUE ${target_${_tname}_description})
248+
else()
249+
message(DEBUG "No target ${_tname}")
250+
endif()
251+
endforeach()
252+
216253
########### PRINT FEATURE SUMMARY ##############
217254
feature_summary(WHAT ALL
218255
INCLUDE_QUIET_PACKAGES
219256
FATAL_ON_MISSING_REQUIRED_PACKAGES
220-
DESCRIPTION "Displaying feature summary\n\nBuild configuration:")
257+
DESCRIPTION "Build configuration:")

tests/CMakeLists.txt

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,6 @@
2626

2727
# Test media path, used by unit tests for input data
2828
file(TO_NATIVE_PATH "${PROJECT_SOURCE_DIR}/examples/" TEST_MEDIA_PATH)
29-
add_definitions( -DTEST_MEDIA_PATH="${TEST_MEDIA_PATH}" )
30-
31-
################### UNITTEST++ #####################
32-
# Find UnitTest++ libraries (used for unit testing)
33-
find_package(UnitTest++)
34-
35-
if (NOT UnitTest++_FOUND)
36-
set(TESTS_ENABLED OFF PARENT_SCOPE)
37-
return()
38-
endif()
39-
40-
# Include UnitTest++ headers (needed for compile)
41-
include_directories(${UnitTest++_INCLUDE_DIRS})
42-
43-
set_package_properties(UnitTest++ PROPERTIES
44-
TYPE RECOMMENDED
45-
PURPOSE "Unit testing framework")
46-
4729

4830
################# BLACKMAGIC DECKLINK ###################
4931
if(ENABLE_BLACKMAGIC)
@@ -56,53 +38,68 @@ if(ENABLE_BLACKMAGIC)
5638
endif()
5739
endif()
5840

59-
############### SET TEST SOURCE FILES #################
60-
set(OPENSHOT_TEST_FILES
61-
Cache_Tests.cpp
62-
Clip_Tests.cpp
63-
Color_Tests.cpp
64-
Coordinate_Tests.cpp
65-
DummyReader_Tests.cpp
66-
ReaderBase_Tests.cpp
67-
ImageWriter_Tests.cpp
68-
FFmpegReader_Tests.cpp
69-
FFmpegWriter_Tests.cpp
70-
Fraction_Tests.cpp
71-
Frame_Tests.cpp
72-
FrameMapper_Tests.cpp
73-
KeyFrame_Tests.cpp
74-
Point_Tests.cpp
75-
QtImageReader_Tests.cpp
76-
Settings_Tests.cpp
77-
Timeline_Tests.cpp)
41+
###
42+
### TEST SOURCE FILES
43+
###
44+
set(OPENSHOT_TESTS
45+
CacheDisk
46+
CacheMemory
47+
Clip
48+
Color
49+
Coordinate
50+
DummyReader
51+
ReaderBase
52+
ImageWriter
53+
FFmpegReader
54+
FFmpegWriter
55+
Fraction
56+
Frame
57+
FrameMapper
58+
KeyFrame
59+
Point
60+
QtImageReader
61+
Settings
62+
Timeline
63+
)
7864

79-
########## SET OPENCV RELATED TEST FILES ###############
65+
###
66+
### OPENCV RELATED TEST FILES
67+
###
8068
if(ENABLE_OPENCV)
81-
list(APPEND OPENSHOT_TEST_FILES
82-
CVTracker_Tests.cpp
83-
CVStabilizer_Tests.cpp
84-
# CVObjectDetection_Tests.cpp
69+
list(APPEND OPENSHOT_TESTS
70+
CVTracker
71+
CVStabilizer
72+
# CVObjectDetection
8573
)
8674
endif()
8775

88-
################ TESTER EXECUTABLE #################
89-
# Create unit test executable (openshot-test)
90-
message (STATUS "Tests enabled, test executable will be built as tests/openshot-test")
76+
###
77+
### Catch2 unit tests
78+
###
79+
if (TESTS_ENABLED)
80+
message (STATUS "Tests enabled, test executables will be compiled")
81+
include(Catch)
9182

92-
add_executable(openshot-test
93-
tests.cpp
94-
${OPENSHOT_TEST_FILES}
95-
)
83+
include(CTest)
9684

97-
# Link libraries to the new executable
98-
target_link_libraries(openshot-test
99-
openshot
100-
${UnitTest++_LIBRARIES}
101-
)
85+
# Create object library for test executable main(),
86+
# to avoid recompiling for every test
87+
add_library(catch-main OBJECT catch_main.cpp)
10288

103-
##### RUNNING TESTS (make os_test / make test) #####
104-
# Hook up the 'make os_test' target to the 'openshot-test' executable,
105-
# if we aren't defining it as the coverage target
106-
if(NOT ENABLE_COVERAGE)
107-
add_custom_target(os_test COMMAND openshot-test)
89+
foreach(tname ${OPENSHOT_TESTS})
90+
add_executable(openshot-${tname}-test ${tname}.cpp $<TARGET_OBJECTS:catch-main>)
91+
target_compile_definitions(openshot-${tname}-test PRIVATE
92+
TEST_MEDIA_PATH="${TEST_MEDIA_PATH}"
93+
)
94+
target_link_libraries(openshot-${tname}-test Catch2::Catch2 openshot)
95+
# Automatically configure CTest targets from Catch2 test cases
96+
catch_discover_tests(
97+
openshot-${tname}-test
98+
TEST_PREFIX ${tname}:
99+
)
100+
list(APPEND CATCH2_TEST_TARGETS openshot-${tname}-test)
101+
endforeach()
102+
# Export target list for coverage use
103+
set(UNIT_TEST_TARGETS ${CATCH2_TEST_TARGETS} PARENT_SCOPE)
108104
endif()
105+

0 commit comments

Comments
 (0)