logo

Minunit

Minunit is a minimal unit test framework for C.
In its initial version, it only was four lines of C, so truly minimalist.

Getting started with minunit

Minunit mostly comprises a pair of macros:

#define MU_ASSERT(message, test) do { if (!(test)) return message; } while (0)
#define MU_RUN_TEST(test) do { char *message = test(); tests_run++; \
                               if (message) return message; } while (0)
extern int tests_run;
Let's write a first toy program, no, not an "Hello, world!" this time:
#include <minunit.h>

int tests_run = 0;

char *test_one_plus_one(void)
{ 
  MU_ASSERT("test 1+1", 1 == 1+1);
  return 0;
}

char *test_suite(void)
{
    MU_RUN_TEST(test_one_plus_one);
    return 0;
}

int main(int ac, char **av)
{
  char *result = test_suite();

  printf("number of tests run: %d\n", tests_run);
  if (result) printf("FAIL: %s\n", result);
  return 0 != result;
}

A few things you have to do in your Unit Test program:

Compiling

Nothing special required here, just indicate the path to minunit.h:
>gcc minunit_test.c -I. -o minunitTest

Why the do ... while(0) ?

As explained in the C languaga FAQ, section 10.4, this is a standard C idiom to write macros comprising several statements.

Extending minunit

Let's see how we can extend minunit.


rf.eerf@sartnap:otliam - v0.02