Skip to content

Commit 425aeb2

Browse files
willcl-arkgithub-actions[bot]
authored andcommitted
CMake: Add dynamic functional test discovery
1 parent 1d09bb0 commit 425aeb2

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

test/CMakeLists.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,45 @@ foreach(script ${functional_tests} fuzz/test_runner.py)
4747
file(CREATE_LINK ${CMAKE_CURRENT_SOURCE_DIR}/${script} ${CMAKE_CURRENT_BINARY_DIR}/${script} COPY_ON_ERROR ${symlink})
4848
endforeach()
4949
unset(functional_tests)
50+
51+
# Functional test discovery
52+
# Generate a CMake script that will be included by CTest to discover functional tests
53+
set(functional_include_file "${CMAKE_CURRENT_BINARY_DIR}/functional_tests_include.cmake")
54+
55+
string(CONCAT functional_include_content
56+
"# Generated functional test discovery for CTest\n"
57+
"# This file is included by CTest to dynamically discover and register functional tests\n"
58+
"\n"
59+
"execute_process(\n"
60+
" COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/functional/list_tests.py\n"
61+
" OUTPUT_VARIABLE functional_test_output\n"
62+
" OUTPUT_STRIP_TRAILING_WHITESPACE\n"
63+
" ERROR_VARIABLE functional_test_error\n"
64+
" ERROR_STRIP_TRAILING_WHITESPACE\n"
65+
" RESULT_VARIABLE functional_test_result\n"
66+
" WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}\n"
67+
")\n"
68+
"\n"
69+
"if(NOT functional_test_result EQUAL 0)\n"
70+
" add_test(bitcoin.functional.DISCOVERY_FAILURE ${Python3_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/functional/list_tests.py)\n"
71+
"else()\n"
72+
" string(REPLACE \"\\n\" \";\" functional_test_lines \"\${functional_test_output}\")\n"
73+
" foreach(test_script IN LISTS functional_test_lines)\n"
74+
" if(test_script MATCHES \"^(.+\\\\.py)$\")\n"
75+
" string(REGEX REPLACE \"\\\\.py$\" \"\" test_name \"\${test_script}\")\n"
76+
" add_test(\"bitcoin.functional.\${test_name}\" ${Python3_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/functional/\${test_script})\n"
77+
" set_tests_properties(\"bitcoin.functional.\${test_name}\" PROPERTIES\n"
78+
" LABELS \"functional\"\n"
79+
" WORKING_DIRECTORY \"${CMAKE_CURRENT_BINARY_DIR}\"\n"
80+
" )\n"
81+
" endif()\n"
82+
" endforeach()\n"
83+
"endif()\n"
84+
)
85+
86+
file(GENERATE
87+
OUTPUT "${functional_include_file}"
88+
CONTENT "${functional_include_content}"
89+
)
90+
91+
set_property(DIRECTORY APPEND PROPERTY TEST_INCLUDE_FILES "${functional_include_file}")

test/functional/list_tests.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2025-present The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or https://opensource.org/license/mit/.
5+
6+
"""
7+
List functional tests for CMake test discovery.
8+
"""
9+
10+
import sys
11+
import os
12+
13+
# Add the current directory to sys.path so we can import test_runner
14+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
15+
16+
try:
17+
import test_runner
18+
# Output all tests (BASE_SCRIPTS + EXTENDED_SCRIPTS)
19+
# all_tests = test_runner.BASE_SCRIPTS + test_runner.EXTENDED_SCRIPTS
20+
all_tests = test_runner.BASE_SCRIPTS
21+
for test in all_tests:
22+
print(test)
23+
except ImportError as e:
24+
print(f"Error importing test_runner: {e}", file=sys.stderr)
25+
sys.exit(1)
26+
except Exception as e:
27+
print(f"Error listing tests: {e}", file=sys.stderr)
28+
sys.exit(1)

0 commit comments

Comments
 (0)