其他分享
首页 > 其他分享> > CUnit单元测试代码实例

CUnit单元测试代码实例

作者:互联网

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "CUnit/Basic.h"
#include "CUnit/TestDB.h"
#include "CUnit/CUnit.h"

int max(int i, int j)
{
    return i>j? 0: -1; 
}

void test1()
{
    CU_ASSERT_EQUAL(max(1,0), 0); 		//正确结果
    CU_ASSERT_EQUAL(max(0,0), -1);		//正确结果
    CU_ASSERT_EQUAL(max(-1,0), -1);		//正确结果
    CU_ASSERT_EQUAL(max(-1,-2), -1);    //错误结果                                                                      
}


CU_TestInfo testcase[] = 
{
    {"result1 is :", test1},
    CU_TEST_INFO_NULL,
};

int suite_init() {return 0;} 
int suite_clean() {return 0;} 

CU_SuiteInfo suites[]=
{
    {"suite1", suite_init, suite_clean, NULL, NULL, testcase},
    CU_SUITE_INFO_NULL,
};

void add_test(void)
{
    assert(NULL != CU_get_registry());
    assert(!CU_is_test_running());

    if(CUE_SUCCESS != CU_register_nsuites(1, suites))
    {   
        printf("error in %d\n", __LINE__);
        assert(1);
    }   
}

int main(int argc, char **argv[])
{
    if (CUE_SUCCESS != CU_initialize_registry())
              return CU_get_error();
    else
    {   
        add_test();
        CU_basic_set_mode(CU_BRM_VERBOSE);
        CU_basic_run_tests();
        CU_cleanup_registry();


    }   
    return 0;
}

编译命令:
gcc -o ctest main_test.c -lcunit

执行结果:
在这里插入图片描述

标签:NULL,CUnit,int,单元测试,实例,return,include,CU
来源: https://blog.csdn.net/dongtaintailiang/article/details/118862429