0

I have been trying out glib for unittesting and have run into trouble. I am interested for a way to not abort the remaining part of the test if a single part fails. I have been trying to do this using g_test_fail() which works but results in a segfault as the test ends. If I comment out g_test_fail() the test is reported as being a success with a normal program termination.

Stepping through the program with gdb I can tell that the test function exits as expected and the fault occurs somewhere in the glib library.

Any ideas?

My example test file is as follows:

//
// Unit Test with GLib
//
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>


typedef struct TestFixture_T {
  unsigned int uiA;
  unsigned int uiB;
  char szString[16];
} TestFixture_T;

void TestFixtureContructor(TestFixture_T* pFixture, gconstpointer pg) {
  pFixture->uiA = 1u;
  pFixture->uiB = 2u;
  strcpy(pFixture->szString, "Blank");
}

void TestFixtureDestructor(TestFixture_T* pFixture, gconstpointer pg) {
  pFixture->uiA = 0u;
  pFixture->uiB = 0u;
  strcpy(pFixture->szString, "");
}

gboolean TestFixtureCompare( TestFixture_T* pFixtureA, TestFixture_T* pFixtureB ) {
  return pFixtureA->uiA == pFixtureB->uiA
    && pFixtureA->uiB == pFixtureB->uiB 
    && strcmp(pFixtureA->szString, pFixtureB->szString) == 0;
}


void test_this(TestFixture_T* pFixtureInput, gconstpointer pg) {
  TestFixture_T LocalFixture;
  TestFixtureContructor( &LocalFixture, NULL );
  if( !TestFixtureCompare( &LocalFixture, pFixtureInput ) ) {
    g_test_fail(); // <-- This is the trouble spot. Comment this out and there is no segfault.
  }
}

int main(int argc, char** argv) {
  /* Initialize test framework */
  g_test_init( &argc, &argv, NULL );

  /* Add test */
  TestFixture_T Fixture;
  g_test_add( "/MyFirstTest", 
      TestFixture_T,
      &Fixture,
      TestFixtureContructor,
      test_this,
      TestFixtureDestructor
      );
  g_test_add( "/MySecondTest", 
      TestFixture_T,
      &Fixture,
      NULL,
      test_this,
      NULL
      );

  int result = g_test_run();

  printf( "...aaaaand scene!\n" );

  return result;
} 
Kenneth
  • 1,167
  • 1
  • 13
  • 26

1 Answers1

1

That's the normal behavior without --keep-going. Even a reduced testcase aborts on first error:

int main(int argc, char **argv) {
    g_test_init(&argc, &argv, NULL);
    g_test_add_func("/fail", g_test_fail);
    return g_test_run();
}

But not if run with --keep-going

Tommi Komulainen
  • 2,830
  • 1
  • 19
  • 16
  • Useful to know but --keep-going does not prevent the segfault when running the program. - No, sorry, actually it does. Just forgot to make clean before I tried it again. – Kenneth May 27 '13 at 09:57