diff --git a/README.md b/README.md index cfbbdba..10bebeb 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ This is a simple C / C++ unit testing framework with no non-standard deps. You can use it in your EECS projects, assuming of course that doing so does not violate the code use, or honor code restrictions, by which you MUST abide. -# Usage +# Basic Usage First, clone the framework into your project folder: git clone https://github.com/michiganhackers/unit-test-framework.git @@ -38,6 +38,53 @@ Run your test suite: That's it! +# Advanced Usage +Maybe you want this to be a little more like JUnit in Java, or you just like organizing a test into its own function, and sharing setup and takedown functions between tests. + +1. Write your before/setup function. By default, a setup function that does nothing is used. + + ``` + void setup(void) { + // Do some setup here, probably set some global variables + } + ``` + +2. Write an after/takedown function. By default, a takedown function that does nothing is used. + + ``` + void takedown(void) { + // Get everything back to a clean slate + } + ``` + +3. Register the callbacks + + ``` + setBeforeFunc(before); + setAfterFunc(after); + ``` + +4. Write some test function + + ``` + void test1() { + // Set some variables using global variables set during setup() + if( assertEqual(something, something_from_setup) ) + test_passed("Yay!"); + else + test_failed("Boo!"); + } + ``` + +5. Run the test + + ``` + runTest(test1, "Test 1"); + ``` + +Now execution of `test1()` is wrapped inbetween calls to `setup()` and `takedown()`. + + # Contributors We (I) would love to get feedback on this, either via issue submissions, or via pull-requests. If you submit a pull-request, be sure to add your name to this @@ -45,6 +92,7 @@ list. + Max Seiden <140dbs@gmail.com> + Ryan Gonzalez ++ David Adrian # License Copyright (C) 2012 Max Seiden <140dbs@gmail.com> diff --git a/unit_test_framework.c b/unit_test_framework.c index 06e9a15..3bc2e3f 100644 --- a/unit_test_framework.c +++ b/unit_test_framework.c @@ -18,18 +18,25 @@ #define _GREEN "\033[32m" #define _YELLOW "\033[33m" #define _WHITE "\033[37m" +#define _BLUE "\x1B[34m" // Helper prototypes static void print_error(const int, const int, const char * const); static _BOOL_RETURN_TYPE negate(const _BOOL_RETURN_TYPE); static int int_cmp(const int*, const int*); static void reset_color(void); +static void setup(); +static void takedown(); // Global static variables static uint32_t num_passed_g; static uint32_t num_failed_g; static uint32_t test_number_g; +// Global static function pointers, set to their defaults +static setup_fn_t test_setup = setup; +static setup_fn_t test_takedown = takedown; + // Test start and end functions void start_suite(const char * const log_msg) { num_passed_g = num_failed_g = test_number_g = 0; @@ -146,6 +153,34 @@ assertIsNotNull(const void* arg1) { return arg1 != NULL ? _TRUE : _FALSE; } +void setBeforeFunc(setup_fn_t setup) { + test_setup = setup; +} + +void setAfterFunc(setup_fn_t takedown) { + test_takedown = takedown; +} + +void runTest(test_fn_t test, const char* const test_name) { + fprintf(stderr, "\n" _BLUE "[Start Test] %s\n", test_name); + reset_color(); + test_setup(); + test(); + test_takedown(); + fprintf(stderr, "\n" _BLUE "[End Test] %s\n", test_name); + reset_color(); +} + +// Default setup function, does nothing +void setup() { + return; +} + +// Default takedown function, does nothing +void takedown() { + return; +} + // Default compare function for ints int int_cmp(const int *arg1, const int *arg2) { return *arg1 - *arg2; @@ -165,3 +200,5 @@ void print_error(const int arg1, const int arg2, const char * const log_msg) { fprintf(stderr, "%s%i %s %i\n", _RED, arg1, log_msg, arg2); reset_color(); } + + diff --git a/unit_test_framework.h b/unit_test_framework.h index e45c06d..cf25aca 100644 --- a/unit_test_framework.h +++ b/unit_test_framework.h @@ -14,6 +14,12 @@ // <1 : arg1 < arg2 | 0 : arg1 == arg2 | >1 : arg1 > arg2 typedef int(*cmp_fn_t)(const void*, const void*); +// Typedefs for setup / takedown / test functions for ease of use. +typedef void (*setup_fn_t)(void); + +// Typedef for a test function +typedef setup_fn_t test_fn_t; + // Output logging helper functions void test_description(const char * const); void test_passed(const char * const); @@ -43,5 +49,10 @@ _BOOL_RETURN_TYPE assertGreaterEqualCmp(const void*, const void*, cmp_fn_t cmp); _BOOL_RETURN_TYPE assertIsNull(const void*); _BOOL_RETURN_TYPE assertNotNull(const void*); +// Prototypes for test runner functions +void setBeforeFunc(setup_fn_t setup); +void runTest(test_fn_t test, const char* const test_name); +void setAfterFunc(setup_fn_t takedown); + #endif