Skip to content

Commit

Permalink
Merge pull request #2 from dadrian/master
Browse files Browse the repository at this point in the history
Add test setup and takedown functionality
  • Loading branch information
mhseiden committed Apr 15, 2013
2 parents 43d59ae + 251768b commit 608f583
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,13 +38,61 @@ 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
list.
+ Max Seiden <[email protected]>
+ Ryan Gonzalez <[email protected]>
+ David Adrian <[email protected]>
# License
Copyright (C) 2012 Max Seiden <[email protected]>
Expand Down
37 changes: 37 additions & 0 deletions unit_test_framework.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}


11 changes: 11 additions & 0 deletions unit_test_framework.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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

0 comments on commit 608f583

Please sign in to comment.