(3)运行测试函数 Main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "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("
Usage: BasicTest [options]
"
"Options: -i ignore framework errors [default].
"
" -f fail on framework error.
"
" -A abort on framework error.
"
" -s silent mode - no output to screen.
"
" -n normal mode - standard output to screen.
"
" -v verbose mode - max output to screen [default].
"
" -e print expected test results and exit.
"
" -h print this message and exit.
");
return 0;
}
}
if (CU_initialize_registry()) {
printf("
Initialization of Test Registry failed.");
}
else {
AddTests();
CU_basic_set_mode(mode);
CU_set_error_action(error_action);
printf("
Tests completed with return value %d.
", CU_basic_run_tests());
CU_cleanup_registry();
}
return 0;
}
(4)Makefile
INC=-I/usr/local/include/CUnit
LIB=-L/usr/local/lib/
all: func.c test_func.c run_test.c
#gcc -o test $(INC) $(LIB) -lcunit $^
gcc -o test $(INC) $(LIB) -lcunit $^
clean:
-rm -rf *.o test
3.3 测试报告
CUnit - A Unit testing framework for C - Version 2.1-0
http://cunit.sourceforge.net/
Suite: suite_success_both
Test: testSuccess1 ... passed
Test: testSuccess2 ... passed
Test: testSuccess3 ... passed
Suite: suite_success_init
Test: testSuccess1 ... passed
Test: testSuccess2 ... passed
Test: testSuccess3 ... passed
Suite: suite_success_clean
Test: testSuccess1 ... passed
Test: testSuccess2 ... passed
Test: testSuccess3 ... passed
Suite: test_failure
Test: testFailure1 ... FAILED
1. ExampleTests.c:52 - 0
Test: testFailure2 ... FAILED
1. ExampleTests.c:53 - 0
Test: testFailure3 ... FAILED
1. ExampleTests.c:54 - 0
WARNING - Suite initialization failed for suite_failure_both.
WARNING - Suite initialization failed for suite_failure_init.
Suite: suite_success_but_failure_clean
Test: testSuiteFailure1 ... FAILED
1. ExampleTests.c:49 - 0
Test: testSuiteFailure2 ... passed
WARNING - Suite cleanup failed for suite_success_but_failure_clean.
Suite: TestSimpleAssert
Test: testSimpleAssert ... FAILED
1. ExampleTests.c:63 - 0
2. ExampleTests.c:64 - !1
3. ExampleTests.c:65 - 0
Test: testFail ... FAILED
1. ExampleTests.c:70 - CU_FAIL("This is a failure.")
2. ExampleTests.c:71 - CU_FAIL("This is another failure.")
Suite: TestBooleanAssert
Test: testAssertTrue ... FAILED
1. ExampleTests.c:79 - CU_ASSERT_TRUE(!CU_TRUE)
2. ExampleTests.c:80 - CU_ASSERT_TRUE(CU_FALSE)
Test: testAssertFalse ... FAILED
1. ExampleTests.c:88 - CU_ASSERT_FALSE(!CU_FALSE)
2. ExampleTests.c:89 - CU_ASSERT_FALSE(CU_TRUE)
Suite: TestEqualityAssert
Test: testAssertEqual ... FAILED
1. ExampleTests.c:99 - CU_ASSERT_EQUAL(10,11)
2. ExampleTests.c:100 - CU_ASSERT_EQUAL(0,1)
3. ExampleTests.c:101 - CU_ASSERT_EQUAL(0,-1)
4. ExampleTests.c:102 - CU_ASSERT_EQUAL(-12,12)
Test: testAssertNotEqual ... FAILED
1. ExampleTests.c:111 - CU_ASSERT_NOT_EQUAL(10,10)
2. ExampleTests.c:112 - CU_ASSERT_NOT_EQUAL(0,-0)
3. ExampleTests.c:113 - CU_ASSERT_NOT_EQUAL(0,0)
4. ExampleTests.c:114 - CU_ASSERT_NOT_EQUAL(-12,-12)
Suite: TestPointerAssert
Test: testAssertPtrEqual ... FAILED
1. ExampleTests.c:121 - CU_ASSERT_PTR_EQUAL((void*)0x100,(void*)0x101)
Test: testAssertPtrNotEqual ... FAILED
1. ExampleTests.c:128 - CU_ASSERT_PTR_NOT_EQUAL((void*)0x100,(void*)0x100)
Suite: TestNullnessAssert
Test: testAssertPtrNull ... FAILED
1. ExampleTests.c:136 - CU_ASSERT_PTR_NULL((void*)0x23)
Test: testAssertPtrNotNull ... FAILED
1. ExampleTests.c:143 - CU_ASSERT_PTR_NOT_NULL(NULL)
2. ExampleTests.c:144 - CU_ASSERT_PTR_NOT_NULL((void*)0x0)
Suite: TestStringAssert
Test: testAssertStringEqual ... FAILED
1. ExampleTests.c:155 - CU_ASSERT_STRING_EQUAL(str1,str3)
2. ExampleTests.c:156 - CU_ASSERT_STRING_EQUAL(str3,str2)
Test: testAssertStringNotEqual ... FAILED
1. ExampleTests.c:168 - CU_ASSERT_STRING_NOT_EQUAL(str1,str2)
Suite: TestNStringAssert
Test: testAssertNStringEqual ... FAILED
1. ExampleTests.c:181 - CU_ASSERT_NSTRING_EQUAL(str2,str3,4)
2. ExampleTests.c:182 - CU_ASSERT_NSTRING_EQUAL(str1,str3,strlen(str1))
Test: testAssertNStringNotEqual ... FAILED
1. ExampleTests.c:194 - CU_ASSERT_NSTRING_NOT_EQUAL(str1,str2,2)
2. ExampleTests.c:195 - CU_ASSERT_NSTRING_NOT_EQUAL(str2,str3,2)
Suite: TestDoubleAssert
Test: testAssertDoubleEqual ... FAILED
1. ExampleTests.c:205 - CU_ASSERT_DOUBLE_EQUAL(10,10.0001,0.00001)
2. ExampleTests.c:206 - CU_ASSERT_DOUBLE_EQUAL(10,10.0001,-0.00001)
3. ExampleTests.c:207 - CU_ASSERT_DOUBLE_EQUAL(-10,-10.0001,0.00001)
4. ExampleTests.c:208 - CU_ASSERT_DOUBLE_EQUAL(-10,-10.0001,-0.00001)
Test: testAssertDoubleNotEqual ... FAILED
1. ExampleTests.c:218 - CU_ASSERT_DOUBLE_NOT_EQUAL(10,10.001,0.01)
2. ExampleTests.c:219 - CU_ASSERT_DOUBLE_NOT_EQUAL(10,10.001,-0.01)
3. ExampleTests.c:220 - CU_ASSERT_DOUBLE_NOT_EQUAL(-10,-10.001,0.01)
4. ExampleTests.c:221 - CU_ASSERT_DOUBLE_NOT_EQUAL(-10,-10.001,-0.01)
Suite: TestFatal
Test: testFatal ... FAILED
1. ExampleTests.c:227 - CU_FALSE
--Run Summary: Type Total Ran Passed Failed
suites 16 14 n/a 3
tests 35 31 10 21
asserts 89 89 47 42
Tests completed with return value 22.
4 项目中CUnit TestCase的作成
4.1 项目中TestCase作成的指导思想
CUnit的TestCase作成的指导思想:TestCase和项目代码分开,作成的TestCase独立地放在其它目录中。