From e6989bcb42c383e81fa5c00ef07d0b1b8def2704 Mon Sep 17 00:00:00 2001 From: Arthur Augusto <59454155+KitsuneSemCalda@users.noreply.github.com> Date: Sun, 1 Nov 2026 11:14:10 -0300 Subject: [PATCH] fix: distinguish test cases from assertions in console summary --- Makefile | 9 ++++++++- README.md | 10 ++++++++++ cest.h | 21 +++++++++++---------- tests/summary.c | 15 +++++++++++++++ tests/summary.sh | 14 ++++++++++++++ 5 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 tests/summary.c create mode 100644 tests/summary.sh diff --git a/Makefile b/Makefile index 8f4754b..7024a90 100644 --- a/Makefile +++ b/Makefile @@ -192,7 +192,14 @@ ifeq ($(OBJC_AVAILABLE),yes) RUNNABLE_EXAMPLES += $(EXAMPLES_OBJC) $(EXAMPLES_OBJCC) endif -run: all +$(BUILD_DIR)/test_summary: tests/summary.c cest.h | $(BUILD_DIR) + $(CC) $(CFLAGS) -DCEST_MAX_TEST_RECORDS=1 $< -o $@ + +.PHONY: test-summary +test-summary: $(BUILD_DIR)/test_summary + sh tests/summary.sh $< + +run: all test-summary @status=0; \ for bin in $(RUNNABLE_EXAMPLES); do \ echo ""; \ diff --git a/README.md b/README.md index 1ab4315..59deaf0 100644 --- a/README.md +++ b/README.md @@ -217,3 +217,13 @@ Detailed documentation is available in multiple languages: ## License This project is licensed under the [BSD-3 Clause License](LICENSE). + +### Test counts and assertion counts + +The console summary reports test cases (`test` / `it`) separately from +assertions (`expect`). A loop containing 100 expectations inside one test +counts as one test and 100 assertions. A test fails if any of its assertions +fails; a filtered test is skipped, and an executed test with no assertions +passes. The existing `cest_stats().passed` and `.failed` fields continue to +count assertions for compatibility. Summary test totals remain accurate even +when `CEST_MAX_TEST_RECORDS` limits the stored report records. diff --git a/cest.h b/cest.h index 64c3c4e..4dccc39 100644 --- a/cest.h +++ b/cest.h @@ -453,9 +453,16 @@ typedef struct { CEST_WEAK cest_test_record_t _cest_test_records[CEST_MAX_TEST_RECORDS] = { {NULL, 0, 0, 0} }; CEST_WEAK int _cest_test_record_count = 0; +// Keep totals independent of the bounded report-record buffer. +CEST_WEAK int _cest_tests_passed = 0; +CEST_WEAK int _cest_tests_failed = 0; static inline void _cest_record_test(const char* name, double time, int failed, int skipped) { CEST_LOCK(); + if (!skipped) { + if (failed) _cest_tests_failed++; + else _cest_tests_passed++; + } if (_cest_test_record_count < CEST_MAX_TEST_RECORDS) { _cest_test_records[_cest_test_record_count].name = name; _cest_test_records[_cest_test_record_count].time = time; @@ -1122,16 +1129,10 @@ static inline int cest_result(void) { _cest_total_time = (double)(clock() - _cest_suite_start_time) / CLOCKS_PER_SEC; printf("\n" CEST_CLR_BOLD "Test Suites Summary:" CEST_CLR_RESET "\n"); - if (_cest_global_stats.failed == 0) { - printf(" " CEST_CLR_GREEN "All %d tests passed!" CEST_CLR_RESET, _cest_global_stats.passed); - if (_cest_global_stats.skipped > 0) printf(" (" CEST_CLR_YELLOW "%d skipped" CEST_CLR_RESET ")", _cest_global_stats.skipped); - printf("\n"); - } else { - printf(" " CEST_CLR_GREEN "Passed: %d" CEST_CLR_RESET, _cest_global_stats.passed); - printf("\n " CEST_CLR_RED "Failed: %d" CEST_CLR_RESET, _cest_global_stats.failed); - if (_cest_global_stats.skipped > 0) printf("\n " CEST_CLR_YELLOW "Skipped: %d" CEST_CLR_RESET, _cest_global_stats.skipped); - printf("\n"); - } + printf(" Tests: %d passed, %d failed, %d skipped\n", + _cest_tests_passed, _cest_tests_failed, _cest_global_stats.skipped); + printf(" Assertions: %d passed, %d failed\n", + _cest_global_stats.passed, _cest_global_stats.failed); #ifdef CEST_ENABLE_LEAK_DETECTION printf("\n" CEST_CLR_BOLD "Memory:" CEST_CLR_RESET "\n"); diff --git a/tests/summary.c b/tests/summary.c new file mode 100644 index 0000000..e93d53d --- /dev/null +++ b/tests/summary.c @@ -0,0 +1,15 @@ +#include "../cest.h" + +int main(int argc, char **argv) { + int negative = argc > 1 && strcmp(argv[1], "--negative") == 0; + cest_init(argc, argv); + test("loop", { + for (int i = 0; i < 100; ++i) expect(i >= 0).toBeTruthy(); + }); + test("empty", { (void)0; }); + test("failure", { + expect(!negative).toBeTruthy(); + expect(!negative).toBeTruthy(); + }); + return cest_result(); +} diff --git a/tests/summary.sh b/tests/summary.sh new file mode 100644 index 0000000..b7317ba --- /dev/null +++ b/tests/summary.sh @@ -0,0 +1,14 @@ +#!/bin/sh +set -eu +binary=$1 +output=$(env -u CEST_POST_RUN "$binary") +printf '%s\n' "$output" | grep -F 'Tests: 3 passed, 0 failed, 0 skipped' +printf '%s\n' "$output" | grep -F 'Assertions: 102 passed, 0 failed' +code=0 +output=$(env -u CEST_POST_RUN "$binary" --negative) || code=$? +test "$code" -eq 1 +printf '%s\n' "$output" | grep -F 'Tests: 2 passed, 1 failed, 0 skipped' +printf '%s\n' "$output" | grep -F 'Assertions: 100 passed, 2 failed' +output=$(env -u CEST_POST_RUN "$binary" empty) +printf '%s\n' "$output" | grep -F 'Tests: 1 passed, 0 failed, 2 skipped' +printf '%s\n' "$output" | grep -F 'Assertions: 0 passed, 0 failed'