Last active
December 9, 2023 18:56
-
-
Save colematt/21e59bee3f2cccef234852df818212ab to your computer and use it in GitHub Desktop.
[CUnit Test Scaffolding] #c #cunit #testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* basic.c | |
* COMPILE: cc -g basic.c -o basic -lcunit | |
*/ | |
#include <CUnit/Basic.h> | |
#include <CUnit/CUnit.h> | |
#include <CUnit/TestRun.h> | |
#include <stdbool.h> | |
enum output { SILENT = 0, NORMAL = 1, VERBOSE = 2 }; | |
/* @brief The suite initialization function. | |
* | |
* @return { Return zero on success, non-zero otherwise } | |
*/ | |
int init_suite(void) { return 0; } | |
/* @brief The suite cleanup function. | |
* | |
* @return { Return zero on success, non-zero otherwise } | |
*/ | |
int clean_suite(void) { return 0; } | |
/** | |
* @brief { A basic test that always passes } | |
*/ | |
void testBASIC(void){ CU_PASS("Test always passes."); } | |
int main(int argc, char *argv[]) { | |
/* Parse output option */ | |
enum output setting = NORMAL; | |
for (size_t i = 1; i < argc; i++) { | |
if (strcmp(argv[i], "--quiet") == 0 || strcmp(argv[i], "--silent") == 0 || | |
strcmp(argv[i], "-q") == 0) | |
setting = SILENT; | |
else if (strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) | |
setting = VERBOSE; | |
else { | |
} | |
} | |
/* initialize the CUnit test registry */ | |
if (CUE_SUCCESS != CU_initialize_registry()) | |
return CU_get_error(); | |
/* add a suite to the registry */ | |
CU_pSuite suite = NULL; | |
suite = CU_add_suite("Basic test suite", init_suite, clean_suite); | |
if (NULL == suite) { | |
CU_cleanup_registry(); | |
return CU_get_error(); | |
} | |
/* Add tests to the suite */ | |
if ((NULL == CU_add_test(suite, "Basic test that always passes", testBASIC)) || | |
(false)) { | |
CU_cleanup_registry(); | |
return CU_get_error(); | |
} | |
/* Run all tests using the CUnit Basic interface */ | |
switch (setting) { | |
case SILENT: | |
CU_basic_set_mode(CU_BRM_SILENT); | |
break; | |
case VERBOSE: | |
CU_basic_set_mode(CU_BRM_VERBOSE); | |
break; | |
case NORMAL: | |
default: | |
CU_basic_set_mode(CU_BRM_NORMAL); | |
} | |
CU_basic_run_tests(); | |
CU_cleanup_registry(); | |
return CU_get_error(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CFLAGS=-g -Wall -std=c99 | |
LDFLAGS= | |
LDLIBS=-lcunit | |
RMFLAGS=-f | |
all: basic | |
.PHONY: clean | |
clean: | |
$(RM) $(RMFLAGS) basic | |
$(RM) $(RMFLAGS) *~ \#*\# *.swp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment