feat: add Unity color and check target

Enable UNITY_OUTPUT_COLOR and UNITY_INCLUDE_PRINT_FORMATTED via
compile definitions on the unity target so colors propagate to all
test suites automatically.

Add a 'check' custom target that builds all suites and runs CTest
with --output-on-failure and USES_TERMINAL, keeping ANSI colors
alive through Ninja's buffering.
This commit is contained in:
2026-05-09 21:32:58 +02:00
parent 701c644408
commit cd19cc14c0
4 changed files with 32 additions and 9 deletions
+2 -1
View File
@@ -15,7 +15,8 @@
```sh ```sh
cmake -S . -B build -G Ninja cmake -S . -B build -G Ninja
ninja -C build # build ninja -C build # build
ninja -C build test # run tests ninja -C build check # run tests (full Unity output, colored)
ninja -C build test # run tests (CTest summary only)
./build/main # run (Linux/macOS) ./build/main # run (Linux/macOS)
./build/main.exe # run (Windows) ./build/main.exe # run (Windows)
``` ```
+6 -8
View File
@@ -26,16 +26,13 @@ ninja -C build
## Test ## Test
```sh ```sh
ninja -C build test # summary output ninja -C build check # full Unity output, colored
ctest --test-dir build -V # verbose (shows each test name) ninja -C build test # CTest summary only
``` ```
Run a single suite directly for the clearest output: `check` builds all suites and runs CTest with `--output-on-failure`,
so assertion-level detail appears on any failure without running
```sh binaries by hand.
./build/tests/test_str.exe
./build/tests/test_report.exe
```
## Run ## Run
@@ -84,6 +81,7 @@ target_include_directories(test_counter PRIVATE "${CMAKE_SOURCE_DIR}")
target_link_libraries(test_counter PRIVATE ctdd_counter Unity::Unity) target_link_libraries(test_counter PRIVATE ctdd_counter Unity::Unity)
target_compile_features(test_counter PRIVATE c_std_23) target_compile_features(test_counter PRIVATE c_std_23)
add_test(NAME test_counter COMMAND test_counter) add_test(NAME test_counter COMMAND test_counter)
list(APPEND TEST_TARGETS test_counter)
``` ```
### 4. Add a stub, confirm RED, then implement GREEN ### 4. Add a stub, confirm RED, then implement GREEN
+7
View File
@@ -55,4 +55,11 @@ if (Unity_INCLUDE_DIR AND TARGET unity)
) )
endif() endif()
if (TARGET unity)
target_compile_definitions(unity PUBLIC
UNITY_OUTPUT_COLOR
UNITY_INCLUDE_PRINT_FORMATTED
)
endif()
set(UNITY_LICENSE_FILE "${unity_SOURCE_DIR}/LICENSE.txt" CACHE FILEPATH "Path to Unity license file") set(UNITY_LICENSE_FILE "${unity_SOURCE_DIR}/LICENSE.txt" CACHE FILEPATH "Path to Unity license file")
+17
View File
@@ -28,12 +28,15 @@ function(cmock_generate_mock target header)
target_include_directories("${target}" PRIVATE "${MOCK_GEN_DIR}" "${header_dir}") target_include_directories("${target}" PRIVATE "${MOCK_GEN_DIR}" "${header_dir}")
endfunction() endfunction()
set(TEST_TARGETS "")
# str tests — pure functions, no mock needed # str tests — pure functions, no mock needed
add_executable(test_str test_str.c) add_executable(test_str test_str.c)
target_include_directories(test_str PRIVATE "${CMAKE_SOURCE_DIR}") target_include_directories(test_str PRIVATE "${CMAKE_SOURCE_DIR}")
target_link_libraries(test_str PRIVATE ctdd_str Unity::Unity) target_link_libraries(test_str PRIVATE ctdd_str Unity::Unity)
target_compile_features(test_str PRIVATE c_std_23) target_compile_features(test_str PRIVATE c_std_23)
add_test(NAME test_str COMMAND test_str) add_test(NAME test_str COMMAND test_str)
list(APPEND TEST_TARGETS test_str)
# report tests — mocks logger.h so log_info calls are intercepted # report tests — mocks logger.h so log_info calls are intercepted
add_executable(test_report test_report.c) add_executable(test_report test_report.c)
@@ -42,6 +45,7 @@ target_link_libraries(test_report PRIVATE ctdd_report Unity::Unity CMock::CMock)
target_compile_features(test_report PRIVATE c_std_23) target_compile_features(test_report PRIVATE c_std_23)
cmock_generate_mock(test_report "${CMAKE_SOURCE_DIR}/ctdd/logger.h") cmock_generate_mock(test_report "${CMAKE_SOURCE_DIR}/ctdd/logger.h")
add_test(NAME test_report COMMAND test_report) add_test(NAME test_report COMMAND test_report)
list(APPEND TEST_TARGETS test_report)
# logger tests — mocks log_write.h so output calls are intercepted # logger tests — mocks log_write.h so output calls are intercepted
add_executable(test_logger test_logger.c) add_executable(test_logger test_logger.c)
@@ -50,3 +54,16 @@ target_link_libraries(test_logger PRIVATE ctdd_logger Unity::Unity CMock::CMock)
target_compile_features(test_logger PRIVATE c_std_23) target_compile_features(test_logger PRIVATE c_std_23)
cmock_generate_mock(test_logger "${CMAKE_SOURCE_DIR}/ctdd/log_write.h") cmock_generate_mock(test_logger "${CMAKE_SOURCE_DIR}/ctdd/log_write.h")
add_test(NAME test_logger COMMAND test_logger) add_test(NAME test_logger COMMAND test_logger)
list(APPEND TEST_TARGETS test_logger)
# 'check' builds all suites and runs CTest with full Unity output.
# USES_TERMINAL keeps ANSI colors alive through Ninja's output buffering.
add_custom_target(check
COMMAND ${CMAKE_CTEST_COMMAND}
--test-dir "${CMAKE_BINARY_DIR}"
--output-on-failure
--progress
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
USES_TERMINAL
DEPENDS ${TEST_TARGETS}
)