Skip to content

Commit 3a6f1a7

Browse files
authored
ESD-1981 code coverage overhaul (#79)
1 parent 327343c commit 3a6f1a7

File tree

1 file changed

+94
-20
lines changed

1 file changed

+94
-20
lines changed

TestTargets.cmake

Lines changed: 94 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# Basic usage is
1010
#
1111
# swift_add_test(test-suite-name
12+
# <UNIT_TEST|INTEGRATION_TEST>
1213
# SRCS
1314
# main.cc
1415
# ...more source files
@@ -24,6 +25,10 @@
2425
# linked against the specified libraries
2526
# - do-test-suite-name - A custom target which will execute the above target
2627
#
28+
# UNIT_TEST or INTEGRATION_TEST options should be specified for each function
29+
# call, it serves to categorize if the test executable is a unit test or an
30+
# integration test. If either option is not specified, a cmake warning will be
31+
# raised. If both are specified, a cmake error will be raised.
2732
#
2833
# LINK, INCLUDE, and WORKING_DIRECTORY are optional parameters.
2934
#
@@ -46,11 +51,12 @@
4651
# from the googletest package. The non-parallel target is always created.
4752
#
4853
# The other function, swift_add_test_runner(), can be used to create a do-...
49-
# target which pointer at some other command. This can be used to invoke a test
54+
# target which points at some other command. This can be used to invoke a test
5055
# which is actually a shell script or some other executable which is available
5156
# but not built from source
5257
#
5358
# swift_add_test_runner(test-suite-name
59+
# <UNIT_TEST|INTEGRATION_TEST>
5460
# COMMAND <testing command> <arguments>
5561
# WORKING_DIRECTORY <path>
5662
# DEPENDS <optional list of cmake targets on which this test depend>
@@ -59,6 +65,8 @@
5965
# This will create just a single target:
6066
# - do-test-suite-name - Execute the given command
6167
#
68+
# UNIT_TEST and INTEGRATION_TEST options work identical to swift_add_test
69+
#
6270
# WORKING_DIRECTORY and DEPENDS are optional arguments. WORKING_DIRECTORY defaults
6371
# to ${CMAKE_CURRENT_BINARY_DIR}
6472
#
@@ -72,7 +80,14 @@
7280
#
7381
# This module will create some extra global targets to build and run all tests
7482
# - build-all-tests - Build all specified tests
75-
# - do-all-tests - Execute all specified tests
83+
# - do-all-tests - Execute all specified tests (includes do-all-unit-tests and
84+
# do-all-integration-tests)
85+
# - do-all-unit-tests - Executes all tests marked with the UNIT_TEST option
86+
# - do-all-integration_tests - Executes all tests marked with the
87+
# INTEGRATION_TEST option
88+
#
89+
# If the PARALLEL option was specified for swift_add_test, than the unit tests
90+
# will be run in parallel when executing do-all-tests.
7691
#
7792
# In addition tests can be added with the option POST_BUILD which will cause
7893
# cmake to execute those tests as part of the 'all' target. To assist this
@@ -96,6 +111,8 @@
96111
# COMMAND <path to executable>
97112
# )
98113
#
114+
# NOTE: using POST_BUILD option is not advised as it will increase build time
115+
#
99116
# Dependency chains are set up so that post build tests will be run towards the
100117
# end of the build process. Cmake lacks functionality to run commands as a
101118
# post-build step so it is not guaranteed that tests will run after everything
@@ -117,29 +134,29 @@ include(CodeCoverage)
117134
option(AUTORUN_TESTS "Automatically run post-build tests as part of 'all' target" ON)
118135

119136
macro(swift_create_test_targets)
120-
if(AUTORUN_TESTS)
121-
set(autorun ALL)
122-
endif()
137+
if(AUTORUN_TESTS)
138+
set(autorun ALL)
139+
endif()
123140

124-
if(NOT TARGET build-all-tests)
125-
add_custom_target(build-all-tests)
126-
endif()
141+
if(NOT TARGET build-all-tests)
142+
add_custom_target(build-all-tests)
143+
endif()
127144

128-
if(NOT TARGET do-all-tests)
129-
add_custom_target(do-all-tests)
130-
endif()
145+
if(NOT TARGET do-all-tests)
146+
add_custom_target(do-all-tests)
147+
endif()
131148

132-
if(NOT TARGET build-post-build-tests)
133-
add_custom_target(build-post-build-tests ${autorun})
134-
endif()
149+
if(NOT TARGET build-post-build-tests)
150+
add_custom_target(build-post-build-tests ${autorun})
151+
endif()
135152

136-
if(NOT TARGET do-post-build-tests)
137-
add_custom_target(do-post-build-tests ${autorun})
138-
endif()
153+
if(NOT TARGET do-post-build-tests)
154+
add_custom_target(do-post-build-tests ${autorun})
155+
endif()
139156
endmacro()
140157

141158
function(swift_add_test_runner target)
142-
set(argOption "POST_BUILD")
159+
set(argOption "INTEGRATION_TEST" "POST_BUILD" "UNIT_TEST")
143160
set(argSingle "COMMENT" "WORKING_DIRECTORY")
144161
set(argMulti "COMMAND" "DEPENDS")
145162

@@ -159,6 +176,12 @@ function(swift_add_test_runner target)
159176
set(wd WORKING_DIRECTORY ${x_WORKING_DIRECTORY})
160177
endif()
161178

179+
if (NOT x_INTEGRATION_TEST AND NOT x_UNIT_TEST)
180+
message(WARNING "Missing INTEGRATION_TEST or UNIT_TEST option")
181+
elseif(x_INTEGRATION_TEST AND x_UNIT_TEST)
182+
message(FATAL_ERROR "Both INTEGRATION_TEST and UNIT_TEST option were specified, you can only specify one")
183+
endif()
184+
162185
add_custom_target(
163186
do-${target}
164187
COMMAND ${x_COMMAND}
@@ -170,6 +193,22 @@ function(swift_add_test_runner target)
170193
add_dependencies(do-${target} ${x_DEPENDS})
171194
endif()
172195

196+
if (x_INTEGRATION_TEST)
197+
if (NOT TARGET do-all-integration-tests)
198+
add_custom_target(do-all-integration-tests)
199+
endif()
200+
201+
add_dependencies(do-all-integration-tests do-${target})
202+
endif()
203+
204+
if (x_UNIT_TEST)
205+
if (NOT TARGET do-all-unit-tests)
206+
add_custom_target(do-all-unit-tests)
207+
endif()
208+
209+
add_dependencies(do-all-unit-tests do-${target})
210+
endif()
211+
173212
if(x_POST_BUILD)
174213
add_custom_target(post-build-${target}
175214
COMMAND ${x_COMMAND}
@@ -186,7 +225,7 @@ function(swift_add_test_runner target)
186225
endfunction()
187226

188227
function(swift_add_test target)
189-
set(argOption "PARALLEL" "POST_BUILD")
228+
set(argOption "INTEGRATION_TEST" "PARALLEL" "POST_BUILD" "UNIT_TEST")
190229
set(argSingle "COMMENT" "WORKING_DIRECTORY")
191230
set(argMulti "SRCS" "LINK" "INCLUDE")
192231

@@ -210,6 +249,12 @@ function(swift_add_test target)
210249
set(wd WORKING_DIRECTORY ${x_WORKING_DIRECTORY})
211250
endif()
212251

252+
if (NOT x_INTEGRATION_TEST AND NOT x_UNIT_TEST)
253+
message(WARNING "Missing INTEGRATION_TEST or UNIT_TEST option")
254+
elseif(x_INTEGRATION_TEST AND x_UNIT_TEST)
255+
message(FATAL_ERROR "Both INTEGRATION_TEST and UNIT_TEST option were specified, you can only specify one")
256+
endif()
257+
213258
add_executable(${target} EXCLUDE_FROM_ALL ${x_SRCS})
214259
swift_set_language_standards(${target})
215260
if(x_INCLUDE)
@@ -238,7 +283,36 @@ function(swift_add_test target)
238283
endif()
239284

240285
add_dependencies(build-all-tests ${target})
241-
add_dependencies(do-all-tests do-${target})
286+
287+
if(x_PARALLEL)
288+
add_dependencies(do-all-tests parallel-${target})
289+
else()
290+
add_dependencies(do-all-tests do-${target})
291+
endif()
292+
293+
if (x_INTEGRATION_TEST)
294+
if (NOT TARGET do-all-integration-tests)
295+
add_custom_target(do-all-integration-tests)
296+
endif()
297+
298+
if(x_PARALLEL)
299+
add_dependencies(do-all-integration-tests parallel-${target})
300+
else()
301+
add_dependencies(do-all-integration-tests do-${target})
302+
endif()
303+
endif()
304+
305+
if (x_UNIT_TEST)
306+
if (NOT TARGET do-all-unit-tests)
307+
add_custom_target(do-all-unit-tests)
308+
endif()
309+
310+
if(x_PARALLEL)
311+
add_dependencies(do-all-unit-tests parallel-${target})
312+
else()
313+
add_dependencies(do-all-unit-tests do-${target})
314+
endif()
315+
endif()
242316

243317
if(x_POST_BUILD)
244318
add_custom_target(

0 commit comments

Comments
 (0)