Abenga
Abenga

Reputation: 71

How do I run unit tests using the GLib framework?

I'm trying to run simple unit tests for some C code I'm writing using GLib. I'm trying to do something like:

#include <math.h>
#include <stdio.h>

#include <glib.h>

static void 
test_stuff () 
{
  g_assert (1 == 1); //Say
}

int main (int argc, char **argv)
{
  g_test_init (&argc, &argv);
  g_test_add_func ("/TestTest", test_stuff);

  return g_test_run();
}

But when I compile (say to a binary called exec) and try to run this using gtester (or even running said binary directly), I get the following error:

me@laptop:tests$ gtester exec
TEST: exec... (pid=6503)

(process:6503): GLib-CRITICAL **: g_test_init: assertion `vararg1 == NULL' failed
FAIL: exec
Terminated

Is there something I'm missing, maybe variables I should pass as I run the tests?

Upvotes: 7

Views: 2368

Answers (1)

unwind
unwind

Reputation: 399949

You're missing an argument to the g_test_init() function. The docs show the prototype as:

void g_test_init(int *argc,
                 char ***argv,
                 ...);

and:

... : Reserved for future extension. Currently, you must pass NULL.

So, you need to pass a NULL as the third argument.

Upvotes: 13

Related Questions