How do I run unit tests using the GLib framework?
Asked Answered
G

1

6

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?

Gemina answered 13/8, 2012 at 9:24 Comment(0)
U
12

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.

Uranometry answered 13/8, 2012 at 9:32 Comment(1)
Yup, and look at your rep now :-) If it works and you accept it, it will help others who read this question in future (just like it helped me ;-)Schnell

© 2022 - 2024 — McMap. All rights reserved.