Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 349
Abenga Avatar asked Aug 13 '12 09:08

Abenga


People also ask

Which framework is used for unit testing?

JUnit is an open source framework you can use to write and run tests. It aims to help develop bug-free and reliable code written in Java. JUnit provides test runners that run tests and assertions to test the expected results.

How do you run unit testing?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).

How does unit testing framework work?

A unit test typically comprises of three stages: plan, cases and scripting and the unit test itself. In the first step, the unit test is prepared and reviewed. The next step is for the test cases and scripts to be made, then the code is tested.


1 Answers

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.

like image 53
unwind Avatar answered Nov 15 '22 05:11

unwind