Writing CUnit Tests

上一篇 / 下一篇  2008-09-19 17:35:19 / 个人分类:CUnit Progammers Guide

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. 

TAG:

李才军的个人空间 引用 删除 李才军   /   2008-09-22 11:46:26
总体来说 翻译的不错,稍加改正就可以了
 

评分:0

我来说两句

Open Toolbar