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: