TestDriver: Add '-N' option to print all tests

This commit is contained in:
Daniel Pfeifer
2026-03-26 10:36:22 -04:00
committed by Brad King
parent b7f83c37a4
commit 4e34426fb3
11 changed files with 102 additions and 0 deletions
+24
View File
@@ -77,3 +77,27 @@ The generated test driver supports the following command-line arguments:
Any additional arguments after ``-A`` are interpreted as exact test names
to skip.
``-N``
.. versionadded:: 4.4
List all available test names (one per line) and exit.
Example
^^^^^^^
.. code-block:: cmake
create_test_sourcelist(SRCS main.c test1.c test2.c)
add_executable(MyTests ${SRCS})
discover_tests(COMMAND MyTests
DISCOVERY_ARGS -N
DISCOVERY_MATCH "^(.+)$"
TEST_NAME "${PROJECT_NAME}.\\1"
TEST_ARGS "\\1"
)
See Also
^^^^^^^^
* :command:`discover_tests`
+4
View File
@@ -5,3 +5,7 @@ discover-tests
discovered at test time by :manual:`ctest(1)`. It runs a user-provided
discovery command, parses its output, and generates test names, arguments,
and properties from regular-expression captures.
* Test drivers generated by the :command:`create_test_sourcelist` command now
support the `-N` option to list all tests, one per line. This is useful in
combination with the new :command:`discover_tests` command.
+9
View File
@@ -88,6 +88,7 @@ int main(int ac, char* av[])
{
int i;
int testNum = 0;
int show_only;
int partial_match;
int run_all;
char *arg;
@@ -117,14 +118,22 @@ int main(int ac, char* av[])
ac--;
av++;
}
show_only = 0;
partial_match = 0;
run_all = 0;
arg = CM_NULL; /* NOLINT */
/* If partial match or running all tests are requested. */
if (testToRun == -1 && ac > 1) {
show_only = (strcmp(av[1], "-N") == 0) ? 1 : 0;
partial_match = (strcmp(av[1], "-R") == 0) ? 1 : 0;
run_all = (strcmp(av[1], "-A") == 0) ? 1 : 0;
}
if (show_only == 1) {
for (i = 0; i < NumTests; ++i) {
printf("%s\n", cmakeGeneratedFunctionMapEntries[i].name);
}
return 0;
}
if (partial_match != 0 && ac < 3) {
printf("-R needs an additional parameter.\n");
return -1;
+1
View File
@@ -412,6 +412,7 @@ add_RunCMake_test(Configure -DMSVC_IDE=${MSVC_IDE})
add_RunCMake_test(CpsExportImportBuild)
add_RunCMake_test(CpsExportImportInstall)
add_RunCMake_test(CpsMapConfig)
add_RunCMake_test(create_test_sourcelist)
add_RunCMake_test(DisallowedCommands)
add_RunCMake_test(discover_tests)
if("${CMAKE_GENERATOR}" MATCHES "Unix Makefiles|Ninja|FASTBuild")
@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.10)
project(${RunCMake_TEST} LANGUAGES NONE)
include(${RunCMake_TEST}.cmake)
@@ -0,0 +1,9 @@
Test project [^
]*/Tests/RunCMake/create_test_sourcelist/Discovery-build
Start 1: case_one
.*
Start 2: case_two
.*
Start 3: case_three
.*
100% tests passed, 0 tests failed out of 3
@@ -0,0 +1,18 @@
enable_language(C)
enable_testing()
create_test_sourcelist(DRIVER_SRCS
main.c
case_one.c
case_two.c
case_three.c
)
add_executable(test_driver ${DRIVER_SRCS})
discover_tests(COMMAND test_driver
DISCOVERY_ARGS -N
DISCOVERY_MATCH ".*"
TEST_NAME "\\0"
TEST_ARGS "\\0"
)
@@ -0,0 +1,19 @@
include(RunCMake)
# Isolate our ctest runs from external environment.
unset(ENV{CTEST_PARALLEL_LEVEL})
unset(ENV{CTEST_OUTPUT_ON_FAILURE})
if(NOT RunCMake_GENERATOR_IS_MULTI_CONFIG)
set(RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Debug)
endif()
function(run_case CASE)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${CASE}-build)
run_cmake(${CASE})
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_command(${CASE}-build ${CMAKE_COMMAND} --build . --config Debug)
run_cmake_command(${CASE}-test ${CMAKE_CTEST_COMMAND} -C Debug)
endfunction()
run_case(Discovery)
@@ -0,0 +1,5 @@
int case_one(int argc, char** argv)
{
(void)argc, (void)argv;
return 0;
}
@@ -0,0 +1,5 @@
int case_three(int argc, char** argv)
{
(void)argc, (void)argv;
return 0;
}
@@ -0,0 +1,5 @@
int case_two(int argc, char** argv)
{
(void)argc, (void)argv;
return 0;
}