zoukankan      html  css  js  c++  java
  • CUnit 的使用

    1 下载 cunit
    2 安装
      到cunit目录
      ./configure
      make
      make install
    3 查看 cunit 库安装信息
      whereis  libcunit.a
      libcunit: /usr/local/lib/libcunit.so /usr/local/lib/libcunit.a /usr/local/lib/libcunit.la
      列出了静态库动态库的路径
    4 查看 头文件 信息
       locate Basic.h
       /usr/local/include/CUnit/Basic.h
    5  使用最简单文件进行测试
     

    /*
    Simple example of a CUnit unit test.

    This program (crudely) demonstrates a very simple "black box"
    test of the standard library functions fprintf() and fread().
    It uses suite initialization and cleanup functions to open
    and close a common temporary file used by the test functions.
    The test functions then write to and read from the temporary
    file in the course of testing the library functions.

    The 2 test functions are added to a single CUnit suite, and
    then run using the CUnit Basic interface. The output of the
    program (on CUnit version 2.0-2) is:

    CUnit : A Unit testing framework for C.
    http://cunit.sourceforge.net/

    Suite: Suite_1
    Test: test of fprintf() ... passed
    Test: test of fread() ... passed

    --Run Summary: Type Total Ran Passed Failed
    suites 1 1 n/a 0
    tests 2 2 2 0
    asserts 5 5 5 0
    /

    nclude <stdio.h>
    nclude <string.h>
    nclude "CUnit/Basic.h"

    Pointer to the file used by the tests. */
    atic FILE* temp_file = NULL;

    The suite initialization function.
    Opens the temporary file used by the tests.
    Returns zero on success, non-zero otherwise.
    /
    t init_suite1(void)

    if (NULL == (temp_file = fopen("temp.txt", "w+"))) {
    return -1;
    }
    else {
    return 0;
    }


    The suite cleanup function.
    Closes the temporary file used by the tests.
    Returns zero on success, non-zero otherwise.
    /
    t clean_suite1(void)

    if (0 != fclose(temp_file)) {
    return -1;
    }
    else {
    temp_file = NULL;
    return 0;
    }


    Simple test of fprintf().
    Writes test data to the temporary file and checks
    whether the expected number of bytes were written.
    /
    id testFPRINTF(void)

    int i1 = 10;

    if (NULL != temp_file) {
    CU_ASSERT(0 == fprintf(temp_file, ""));
    CU_ASSERT(2 == fprintf(temp_file, "Q\n"));
    CU_ASSERT(7 == fprintf(temp_file, "i1 = %d", i1));
    }


    Simple test of fread().
    Reads the data previously written by testFPRINTF()
    and checks whether the expected characters are present.
    Must be run after testFPRINTF().
    /
    id testFREAD(void)

    unsigned char buffer[20];

    if (NULL != temp_file) {
    rewind(temp_file);
    CU_ASSERT(9 == fread(buffer, sizeof(unsigned char), 20, temp_file));
    CU_ASSERT(0 == strncmp(buffer, "Q\ni1 = 10", 9));
    }


    The main() function for setting up and running the tests.
    Returns a CUE_SUCCESS on successful running, another
    CUnit error code on failure.
    /
    t main()

    CU_pSuite pSuite = NULL;

    /* initialize the CUnit test registry */
    if (CUE_SUCCESS != CU_initialize_registry())
    return CU_get_error();

    /* add a suite to the registry */
    pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
    if (NULL == pSuite) {
    CU_cleanup_registry();
    return CU_get_error();
    }

    /* add the tests to the suite */
    /* NOTE - ORDER IS IMPORTANT - MUST TEST fread() AFTER fprintf() */
    if ((NULL == CU_add_test(pSuite, "test of fprintf()", testFPRINTF)) ||
    (NULL == CU_add_test(pSuite, "test of fread()", testFREAD)))
    {
    CU_cleanup_registry();
    return CU_get_error();
    }

    /* Run all tests using the CUnit Basic interface */
    CU_basic_set_mode(CU_BRM_VERBOSE);
    CU_basic_run_tests();
    CU_cleanup_registry();
    return CU_get_error();


    6 生成目标文件

    生成目标文件 
    gcc -c simple.c
    链接成可执行文件
    gcc -static -o simple simple.o /usr/local/lib/libcunit.a -lm

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

    Suite: Suite_1
      Test: test of fprintf() ...passed
      Test: test of fread() ...passed

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

    Elapsed time =    0.000 seconds

  • 相关阅读:
    java struts2入门学习实例--用户注册
    java struts2入门学习实例--将客户端IP地址和访问方式输出到浏览器
    struts2基本配置详解2
    struts2基本配置详解
    使用maven+eclipse搭建最简单的struts2的HelloWorld
    2013年总结
    linux shell 脚本攻略学习20--awk命令入门详解
    linux shell 脚本攻略学习19--sed命令详解
    linux shell 脚本攻略学习18--grep命令详解
    linux shell 脚本攻略学习17--正则表达式入门
  • 原文地址:https://www.cnblogs.com/hbb0b0/p/2155104.html
Copyright © 2011-2022 走看看