CUnit 断言断言`((void *)0) != f_pCurSuite' 失败

CUnit assertion Assertion `((void *)0) != f_pCurSuite' failed

我的代码如下所示:

#include <CUnit/CUnit.h>


int maxi(int i1, int i2)
{
    return (i1 > i2) ? i1 : i2;
}

void test_maxi(void)
{
    CU_ASSERT(maxi(0,2) == 2);
}

int main() {
    test_maxi();
    return 0;
}

我在 Ubuntu 上使用 gcc test.c -o test -lcunit 编译了它。

我在尝试启动时遇到此错误:

test: TestRun.c:159: CU_assertImplementation: Assertion `((void *)0) != f_pCurSuite' failed. Aborted (core dumped)

这是什么意思?我在互联网上一无所获。

CUnit 适用于测试套件,您需要先创建才能运行 应用程序。

使测试正常工作的一种非常基本的方法如下所示:

#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>

int maxi(int i1, int i2)
{
    return (i1 > i2) ? i1 : i2;
}

void test_maxi(void)
{
    CU_ASSERT(maxi(0,2) == 2);
}

int main() {
    CU_initialize_registry();
    CU_pSuite suite = CU_add_suite("maxi_test", 0, 0);

    CU_add_test(suite, "maxi_fun", test_maxi);

    CU_basic_set_mode(CU_BRM_VERBOSE);
    CU_basic_run_tests();
    CU_cleanup_registry();

    return 0;
}

没有所有必需的检查,但正如 Joachim Pileborg 在评论中所建议的那样,遵循提供的示例代码会更安全。