Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""; \
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
21 changes: 11 additions & 10 deletions cest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
15 changes: 15 additions & 0 deletions tests/summary.c
Original file line number Diff line number Diff line change
@@ -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();
}
14 changes: 14 additions & 0 deletions tests/summary.sh
Original file line number Diff line number Diff line change
@@ -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'
Loading