Skip to content

Commit df8c5b2

Browse files
authored
build each test file as a separate executable (#1017)
* build each test file as a separate executable * update docs * add compat entrypoint for tests
1 parent 4984a57 commit df8c5b2

File tree

4 files changed

+105
-31
lines changed

4 files changed

+105
-31
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,20 @@ the need to store large test vector files in git, and instead can be generated a
178178

179179
To run the unit tests, from the cmake build directory run:
180180
```sh
181-
test/matx_test
181+
make -j test
182182
```
183183

184-
This will execute all unit tests defined. If you wish to execute a subset of tests, or run with different options, you
185-
may run test/matx_test directly with parameters defined by [Google Test](https://github.com/google/googletest). To run matx_test
186-
directly, you must be inside the build/test directory for the correct paths to be set. For example,
187-
to run only tests with the name FFT:
184+
This will execute all unit tests defined. It is also possible to build and execute a single test, for example:
185+
```
186+
make test_00_operators_interp_test
187+
test/test_00_operators_interp_test
188+
```
189+
190+
To run a subset of tests, it is possible to use [ctest](https://cmake.org/cmake/help/latest/manual/ctest.1.html) from inside the `build/test` directory. For example, to run only tests with the name FFT:
188191

189192
```sh
190193
cd build/test
191-
./matx_test --gtest_filter="*FFT*"
194+
ctest -R "FFT"
192195
```
193196

194197

docs/_sources/basics/build.rst

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,20 @@ To build unit tests, pass the argument ``-DMATX_BUILD_TESTS=ON`` to CMake to con
8888
8989
make -j test
9090
91-
This will compile and run all unit tests. For more control over which tests to run, you may run test/matx_test directly with parameters
92-
defined by Google Test (https://github.com/google/googletest). To run matx_test directly, you must be inside the build/test directory
93-
for the correct paths to be set. For example, to run only tests with the name FFT:
91+
This will execute all unit tests defined. It is also possible to build and execute a single test, for example:
9492

9593
.. code-block:: shell
9694
97-
test/matx_test --gtest_filter="*FFT*"
95+
make test_00_operators_interp_test
96+
test/test_00_operators_interp_test
97+
98+
To run a subset of tests, it is possible to use `ctest <https://cmake.org/cmake/help/latest/manual/ctest.1.html>`_ from inside the ``build/test`` directory. For example, to run only tests with the name FFT:
99+
100+
.. code-block:: shell
101+
102+
cd build/test
103+
ctest -R "FFT"
104+
98105
99106
Examples
100107
--------

test/CMakeLists.txt

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ set (test_sources
4747
00_sparse/Matmul.cu
4848
00_sparse/Matvec.cu
4949
00_sparse/Solve.cu
50-
main.cu
5150
)
5251

5352
# Some of <00_io> tests need csv files and binaries which all
@@ -85,30 +84,76 @@ endforeach()
8584
set(target_inc ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/../examples/ ${proprietary_inc_list})
8685
set(system_inc ${CUTLASS_INC} ${GTEST_INC_DIRS} ${pybind11_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS})
8786

88-
set(all_test_srcs ${test_sources} ${proprietary_sources})
87+
# Function to create individual test executables
88+
function(create_test_executable test_file)
89+
# Get the filename without path and extension for the target name
90+
get_filename_component(test_name ${test_file} NAME_WE)
91+
# Get the directory to make target names unique
92+
get_filename_component(test_dir ${test_file} DIRECTORY)
93+
string(REPLACE "/" "_" test_dir_clean ${test_dir})
94+
set(target_name "test_${test_dir_clean}_${test_name}")
8995

90-
add_executable(matx_test main.cu ${all_test_srcs})
96+
# Create executable with main.cu and the specific test file
97+
add_executable(${target_name} main.cu ${test_file})
9198

92-
# Set all the flags/other properties
93-
set_property(TARGET matx_test PROPERTY ENABLE_EXPORTS 1)
99+
# Set all the flags/other properties
100+
set_property(TARGET ${target_name} PROPERTY ENABLE_EXPORTS 1)
94101

95-
if (DEFINED cupy_PYTHON_PACKAGE)
96-
target_compile_definitions(matx_test PRIVATE CUPY_INSTALLED)
97-
endif()
102+
if (DEFINED cupy_PYTHON_PACKAGE)
103+
target_compile_definitions(${target_name} PRIVATE CUPY_INSTALLED)
104+
endif()
98105

99-
if (MSVC)
100-
target_compile_options(matx_test PRIVATE /W4 /WX)
101-
else()
102-
target_compile_options(matx_test PRIVATE ${WARN_FLAGS})
103-
#target_compile_options(matx_test PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:${WARN_FLAGS}>)
104-
target_compile_options(matx_test PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:${MATX_CUDA_FLAGS}>)
105-
endif()
106+
if (MSVC)
107+
target_compile_options(${target_name} PRIVATE /W4 /WX)
108+
else()
109+
target_compile_options(${target_name} PRIVATE ${WARN_FLAGS})
110+
target_compile_options(${target_name} PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:${MATX_CUDA_FLAGS}>)
111+
endif()
106112

107-
target_include_directories(matx_test PRIVATE "${target_inc}")
108-
target_include_directories(matx_test SYSTEM PRIVATE "${system_inc}")
109-
target_link_libraries(matx_test PRIVATE matx::matx) # Transitive properties
110-
target_link_libraries(matx_test PRIVATE ${NVSHMEM_LIBRARY} gtest)
113+
target_include_directories(${target_name} PRIVATE "${target_inc}")
114+
target_include_directories(${target_name} SYSTEM PRIVATE "${system_inc}")
115+
target_link_libraries(${target_name} PRIVATE matx::matx) # Transitive properties
116+
target_link_libraries(${target_name} PRIVATE ${NVSHMEM_LIBRARY} gtest)
111117

118+
# Register the test with CTest
119+
add_test(NAME ${target_name} COMMAND ${target_name})
120+
set_tests_properties(${target_name} PROPERTIES
121+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
122+
)
123+
endfunction()
124+
125+
# Enable CTest
126+
enable_testing()
127+
128+
# Create individual executables for each test file
129+
foreach(test_file ${test_sources})
130+
create_test_executable(${test_file})
131+
endforeach()
132+
133+
# Create individual executables for proprietary tests
134+
foreach(test_file ${proprietary_sources})
135+
create_test_executable(${test_file})
136+
endforeach()
137+
138+
# Number of test jobs to run in parallel
139+
set(CTEST_PARALLEL_JOBS 4)
140+
141+
# Create a legacy matx_test script for CI compatibility
142+
configure_file(
143+
${CMAKE_CURRENT_SOURCE_DIR}/matx_test.sh
144+
${CMAKE_CURRENT_BINARY_DIR}/matx_test
145+
COPYONLY
146+
)
147+
# Make the script executable
148+
file(CHMOD ${CMAKE_CURRENT_BINARY_DIR}/matx_test
149+
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
150+
GROUP_READ GROUP_EXECUTE
151+
WORLD_READ WORLD_EXECUTE)
152+
153+
# Add a custom target to run CTest from the main build directory
112154
add_custom_target(test
113-
DEPENDS matx_test
114-
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/matx_test)
155+
COMMAND ${CMAKE_CTEST_COMMAND} -j${CTEST_PARALLEL_JOBS} --output-on-failure
156+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
157+
COMMENT "Running all MatX tests in parallel (${CTEST_PARALLEL_JOBS} cores)"
158+
VERBATIM
159+
)

test/matx_test.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
echo "MatX Test Wrapper - Running CTest in parallel..."
4+
5+
# Change to the directory where this script is located (test directory)
6+
cd "$(dirname "$0")"
7+
8+
# Build the ctest command with parallel jobs and output on failure
9+
CTEST_CMD="ctest -j4 --output-on-failure"
10+
11+
# Forward any additional arguments to ctest
12+
if [ $# -gt 0 ]; then
13+
CTEST_CMD="$CTEST_CMD $*"
14+
fi
15+
16+
echo "Executing: $CTEST_CMD"
17+
18+
# Execute ctest and preserve its exit code
19+
exec $CTEST_CMD

0 commit comments

Comments
 (0)