发布新日志

  • Writing CUnit Tests

    2008-09-19 17:35:19

    2. Writing CUnit Test Cases
    编写CUnit测试用例
    2.1. Test Functions
    测试函数
    A CUnit "test" is a C function having the signature:
    一个CUnit 测试是一个有C函数标识

    void test_func(void)

    There are no restrictions on the content of a test function, except that it should not modify the CUnit framework (e.g. add suites or tests, modify the test registry, or initiate a test run).
    在测试函数的内容上没有任何限制,除了他们修改CUnit 的框架(例如:增加组件或测试用例,或者修改一个测试注册表,或者初始化运行一个测试用例)
    A test function may call other functions (which also may not modify the framework).
    一个测试函数可能调用其他的函数
    Registering a test will cause it's function to be run when the test is run.
    当测试运行时,注册的测试会导致这个函数也在运行

    An example test function for a routine that returns the maximum of 2 integers might look like:

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

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


    2.2. CUnit Assertions
        CUnit 断言
    CUnit provides a set of assertions for testing logical conditions.
    CUnit 为逻辑条件提供丰富的断言
    The success or failure of these assertions is tracked by the framework, and can be viewed when a test run is complete.
    通过这个框架跟踪到断言的成功或者失败,并且当这个测试运行接手能够看到视图
    Each assertion tests a single logical condition, and fails if the condition evaluates to FALSE.
    每个断言只测试一个逻辑条件,如果条件被赋值为FALSE为失败
    Upon failure, the test function continues unless the user chooses the 'xxx_FATAL' version of an assertion.
    一旦失败,测试函数继续测试,除非用户选择"xxx_FATAL"版本的断言
    In that case, the test function is aborted and returns immediately.
    在这种情况下,测试函数中途失败立刻返回结果
    FATAL versions of assertions should be used with caution!
    在FATAL 版本的断言应该谨慎使用
    There is no opportunity for the test function to clean up after itself once a FATAL assertion fails.
    没有任何机会释放测试函数,在他本身经历过一次致命的断言失败
    The normal suite cleanup function is not affected, however.
    正常的suite销毁函数不会收到影响,尽管如此,
    There are also special "assertions" for registering a pass or fail with the framework without performing a logical test.
    也有特殊的断言,通过注册或者在失败的框架下,没有执行逻辑测试
    These are useful for testing flow of control or other conditions not requiring a logical test:
    这些都是有用的测试控制流程或者其他一些条件不需要逻辑测试:

        void test_longjmp(void)
        {
          jmp_buf buf;
          int i;

          i = setjmp(buf);
          if (i == 0) {
            run_other_func();
            CU_PASS("run_other_func() succeeded.");
          }
          else
            CU_FAIL("run_other_func() issued longjmp.");
        }

    Other functions called by a registered test function may use the CUnit assertions freely.
    These assertions will be counted for the calling function.
    They may also use FATAL versions of assertions - failure will abort the original test function and its entire call chain. 
  • Introduction to Unit Testing with CUnit

    2008-09-19 12:11:31

    1. Introduction to Unit Testing with CUnit
    介绍单元测试和CUnit
    1.1. Descrīption
    描述
    CUnit is a system for writing, administering, and running unit tests in C.
    CUnit 可以在C的项目上编写,管理,运行单元测试,
    It is built as a static library which is linked with the user's testing code.
    它建立一个静态库和用户的代码链接在一起
    CUnit uses a simple framework for building test structures, and provides a rich set of assertions for testing common data types.
    CUnit 用了一个简单的框架来建立测试结构, 并且为常用的数据类型提供了丰富的断言语句支持。
    In addition, several different interfaces are provided for running tests and reporting results.
    此外.可以为测试运行和测试报告提供不同的接口
    These include automated interfaces for code-controlled testing and reporting, as well as interactive interfaces allowing the user to run tests and view results dynamically.
    还包括控制代码的测试和报告的自动化界面,以及交互式界面也考虑到用户测试的运行和动态结果视图
    The data types and functions useful to the typical user are declared in the following header files:
    典型的用户用到数据类型和功能会使用以下头文件:
    Header File     Descrīption
    #include <CUnit/CUnit.h>     ASSERT macros for use in test cases, and includes other framework headers.
                    ASSERT 宏用于测试用例也包括其他框架头文件
    #include <CUnit/CUError.h>     Error handing functions and data types. Included automatically by CUnit.h.
                    
    #include <CUnit/TestDB.h>     Data type definitions and manipulation functions for the test registry, suites, and tests. Included automatically by CUnit.h.
    #include <CUnit/TestRun.h>     Data type definitions and functions for running tests and retrieving results. Included automatically by CUnit.h.
    #include <CUnit/Automated.h>     Automated interface with xml output.
                    自动化界面与xml 输出
    #include <CUnit/Basic.h>     Basic interface with non-interactive output to stdout.
                    基础界面与非交互式标准输出
    #include <CUnit/Console.h>     Interactive console interface.
                    交互式控制台界面
    #include <CUnit/CUCurses.h>     Interactive console interface (*nix).
                    交互式控制台界面(unix)
    #include <CUnit/Win.h>     Windows interface (not yet implemented).
                    windows界面(没有实现)

    1.2. Structure
    结构
    CUnit is a combination of a platform-independent framework with various user interfaces.
    CUnit 是一个结合各种用户界面的独立平台框架
    The core framework provides basic support for managing a test registry, suites, and test cases.
    该框架核心提供了基本的支持,管理一个测试的注册表,组件,测试用例.
    The user interfaces facilitate interaction with the framework to run tests and view results.
    用户界面容易在交互式作用的框架内进行测试和查看结果
    CUnit is organized like a conventional unit testing framework:
    CUnit 的组织像传统的单元测试框架

                          Test Registry
                                |
                 ------------------------------
                 |                            |
              Suite '1'      . . . .       Suite 'N'
                 |                            |
           ---------------             ---------------
           |             |             |             |
        Test '11' ... Test '1M'     Test 'N1' ... Test 'NM'

    Individual test cases are packaged into suites, which are registered with the active test registry.
    个别几个测试用例封装到suites里面,该组件被注册,并且激活该注册表
    Suites can have setup and teardown functions which are automatically called before and after running the suite's tests.
    在运行Suite测试前后,suite能够自动调用装载和卸载的功能,
    All suites/tests in the registry may be run using a single function call, or selected suites or tests can be run.
    所有的suites/测试都有可能执行一个简单的功能或者选择一个suites 或者执行一个测试用例调用注册表

    1.3. General Usage
    A typical sequence of steps for using the CUnit framework is:
    使用CUnit的一个典型步骤是:
       1. Write functions for tests (and suite init/cleanup if necessary).
         编写一个测试函数 (suite 初始化和销毁)
       2. Initialize the test registry - CU_initialize_registry()
        初始化测试注册表
       3. Add suites to the test registry - CU_add_suite()
        添加suites 到注册表
       4. Add tests to the suites - CU_add_test()
        添加测试用例到注册表
       5. Run tests using an appropriate interface, e.g. CU_console_run_tests
        运行测试用例使用一个合适的界面
       6. Cleanup the test registry - CU_cleanup_registry
        释放测试注册表
    1.4. Changes to the CUnit API in Version 2
         转到第2版,CUnit API
    All public names in CUnit are now prefixed with 'CU_'.
    在CUnit中,现在所有公共的名字都要加上前缀"CU_"
    This helps minimize clashes with names in user code.
    在用户代码中,有助于减少名字相同
    Note that earlier versions of CUnit used different names without this prefix.
    注意在早期版本CUnit使用不同的名字,没有前缀
    The older API names are deprecated but still supported.
    老的API,仍然支持
    To use the older names, user code must now be compiled with USE_DEPRECATED_CUNIT_NAMES defined.
    使用旧的名称,用户必须在编译代码的是定义 USE_DEPRECATED_CUNIT_NAMES
    The deprecated API functions are described in the appropriate sections of the documentation.
    在部分文档中描述废弃的API
Open Toolbar