0

我一直在尝试 glib 进行单元测试,但遇到了麻烦。如果单个部分失败,我对不中止测试的其余部分的方法很感兴趣。我一直在尝试使用 g_test_fail() 来做到这一点,它可以工作,但在测试结束时会导致段错误。如果我注释掉 g_test_fail() ,则测试报告为成功,程序正常终止。

使用 gdb 单步执行程序,我可以看出测试函数按预期退出,并且故障发生在 glib 库中的某处。

有任何想法吗?

我的示例测试文件如下:

//
// 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;
} 
4

1 回答 1

1

这是没有--keep-going. 即使是减少的测试用例也会在第一个错误时中止:

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

但如果与--keep-going

于 2013-05-25T12:37:03.813 回答