CUnit开发环境搭建过程可能遇到的一些问题及解决方法

来源:互联网 发布:手机淘宝pc端在哪里 编辑:程序博客网 时间:2024/06/05 01:08

CUnit提供一个统一的C语言单元测试框架,是开发过程比较实用的工具。

今天尝试在自己的虚拟机上搭建CUnit的测试环境,搭建过程中遇到些问题,在网上找了一些解决方案但都没有凑效。最终的解决方案在网上也没有提及,记录下来,留给后续可能需要的人。

一、 CUnit的获取、编译与安装

这一部分网上资料很丰富,大部分过程都是一样的:

1.获取软件包

2. # tar -xvf CUnit-2.1-2-src.tar

3. # cd CUnit-2.1-2

4. # ./configure

5. make

6.make install

7.  #cd /usr/local/lib

 

 

  #ldconfig

重点:

CUnit依赖于 curses类库,需要先安装curses

二、测试范例

CUnit安装完毕之后,使用网上流行的案例对安装好的Cunit进行验证:

1. 生成待验证函数 int Max(int i , int j) /*这个大家都会*/

2. 测试用例 testcase.c 采用网上流行的版本,代码贴在最后,这里重点介绍大部分网站的代码中的几点错误:

     a)testcase.c 中extern int maxi(int i, int j); 连接过程会出错 。 解决方案:将待测函数原型的声明放到头文件中,testcase.c包含此头文件即可

     b)网上的代码中,“,”都是中文字符,需要修改为英文字符

3. 测试主文件main.c  

     a)  代码中“,”都是使用的中文字符,需要使用英文字符替换

    b) #include "Basic.h"  编译时找不到头文件。 解决方案:使用#include "CUnit\Basic.h"

    c) main函数中 所以的if分支条件 应该为 if(!strcmp("-x", argv[i]))

   

4. Makefile

    a)  所有的冒号都是中文字符,应该修改为英文字符

    b)  执行make时候,显示CU_assertImplementation、CU_assertImplementation 等CUnit的函数找不着

         原因:makefile中 -lcunit选项放在输入文件之前

        修改方案: 将 -lcunit选项放在 输入文件列表之后

   c) 做了b)中的修改之后,执行make,显示scrollok、nocbreak、stdscr等符号找不到

       原因:CUnit安装没有问题的话,此问题由于gcc编译时没有包含curses库

      解决方案: gcc编译时,-lcunit之后 添加 -lcurses

附修改后的example code:

1. 待测函数 max.h max.c

//max.h

#ifndef _MAX_H_H
#define _MAX_H_H


int Max(int i,int j);


#endif

//max.c

#include "max.h"

int Max(int i,int j)
{
    return (i>=j) ? i :j;
}

//testcase.c

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

void testIQJ()
{
    CU_ASSERT_EQUAL(Max(1,1),1);
    CU_ASSERT_EQUAL(Max(0,-0),0);
}


void testIGJ()
{
    CU_ASSERT_EQUAL(Max(2,1),2);
    CU_ASSERT_EQUAL(Max(0,-1),0);
    CU_ASSERT_EQUAL(Max(-1,-2),-1);
}


void testILJ()
{
    CU_ASSERT_EQUAL(Max(1,2),2);
    CU_ASSERT_EQUAL(Max(-1,0),0);
    CU_ASSERT_EQUAL(Max(-2,-1),-1);
}

CU_TestInfo testcases[] =
{
    {"Testing i equals j.",testIQJ},
    {"Testing i greater than j.", testIGJ},
    {"Testing i less than j.", testILJ},
    CU_TEST_INFO_NULL
};

int suite_success_init(void)
{
    return 0;
}


int suite_success_clean(void)
{
    return 0;
}


CU_SuiteInfo suites[] =

{
{"Testing the function maxi.", suite_success_init, suite_success_clean, testcases},
    CU_SUITE_INFO_NULL
};

void AddTests(void)
{
    assert(NULL != CU_get_registry());
    assert(!CU_is_test_running());
    if(CUE_SUCCESS != CU_register_suites(suites))
    {
        fprintf(stderr, "Register suites failed - %s ", CU_get_error_msg());
        exit(EXIT_FAILURE);
    }
}

//main.c

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

int main(int argc, char* argv[])
{
    CU_BasicRunMode mode = CU_BRM_VERBOSE;
    CU_ErrorAction error_action = CUEA_IGNORE;
    int i;
    setvbuf(stdout, NULL, _IONBF, 0);
    for (i=1 ; i<argc ; i++) {
        if (!strcmp("-i", argv[i]))
        {
            error_action = CUEA_IGNORE;
        }
        else if (!strcmp("-f", argv[i]))
        {
            error_action = CUEA_FAIL;
        }
        else if (!strcmp("-A", argv[i]))
        {
            error_action = CUEA_ABORT;
        }
        else if (!strcmp("-s", argv[i]))
        {
            mode = CU_BRM_SILENT;
        }
        else if (!strcmp("-n", argv[i]))
        {
            mode = CU_BRM_NORMAL;
        }
        else if (!strcmp("-v", argv[i]))
        {
            mode = CU_BRM_VERBOSE;
        }
        else if (!strcmp("-e", argv[i]))
        {
           return 0;
        }
        else
        {
            printf("\nUsage. BasicTest [options]\n\n"
                   "Options. -i ignore framework errors [default].\n"
                   " -f fail on framework error.\n"
                   " -A abort on framework error.\n\n"
                   " -s silent mode - no output to screen.\n"
                   " -n normal mode - standard output to screen.\n"
                   " -v verbose mode - max output to screen [default].\n\n"
                   " -e print expected test results and exit.\n"
                   " -h print this message and exit.\n\n");
            return 0;
        }
    }
    if (CU_initialize_registry())
    {
        printf("\nInitialization of Test Registry failed.");
    }
    else
    {
        AddTests();
        CU_basic_set_mode(mode);
        CU_set_error_action(error_action);
        printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
        CU_cleanup_registry();
    }
    return 0;
}

//makefile

INC=-I/usr/local/include/CUnit
LIB=-L/usr/local/lib/
all:max.c testcase.c main.c
#gcc -o test $(INC) $(LIB) -lcunit $^
        gcc -o test $(INC) $(LIB) $^ -lcunit -lcurses
clean:
        rm -rf *.o test

执行结果:

root@ubuntu:/opt/cunit/project# ./test


     CUnit - A unit testing framework for C - Version 2.1-2
     http://cunit.sourceforge.net/


Suite: Testing the function maxi.
  Test: Testing i equals j. ...passed
  Test: Testing i greater than j. ...passed
  Test: Testing i less than j. ...passed

Run Summary:    Type  Total    Ran Passed Failed Inactive
              suites      1      1    n/a      0        0
               tests      3      3      3      0        0
             asserts      8      8      8      0      n/a

Elapsed time =    0.000 seconds

Tests completed with return value 0.

 

原创粉丝点击