OCUnit 测试用例未运行
Posted
技术标签:
【中文标题】OCUnit 测试用例未运行【英文标题】:OCUnit test cases not running 【发布时间】:2013-05-21 14:32:11 【问题描述】:如果我创建一个包含单元测试的新项目,则会调用测试用例。但是,当我向现有项目添加测试时,我会在日志中看到以下内容:
2013-05-21 19:41:08.814 otest[51593:7e03] Unknown Device Type. Using UIUserInterfaceIdiomPhone based on screen size Test Suite '/Users/impadmin/Library/Developer/Xcode/DerivedData/SmartDiary-frrybdtgyyjgewbialqsmxkwvlxs/Build/Products/Debug-iphonesimulator/testSmartDiary.octest(Tests)'
开始于 2013-05-21 14:11:09 +0000 测试套件“testShowContacts”开始于 2013-05-21 14:11:09 +0000 测试套件“testShowContacts”于 2013-05-21 14:11:09 +0000 完成。 在 0.000 (0.000) 秒内执行了 0 次测试,其中 0 次失败(0 次意外)
我已将SenTestKit
添加到框架中,然后通过添加目标添加了测试包。
我哪里错了?
让我在这里添加方法及其测试用例:
//要测试的方法
-(IBAction)addTask
if(!([mPriority.text isEqualToString:@"1"] ||[mPriority.text isEqualToString:@"2"] ||[mPriority.text isEqualToString:@"3"] ||[mPriority.text isEqualToString:@"4"] ||[mPriority.text isEqualToString:@"5"]))
NSLog(@"Enter valid Priority value");
else if ([mTaskName.text isEqualToString:@""])
NSLog(@"Task name can't be blank");
else
sqlite3_stmt *statement;
const char *dbPath = [mDatabasePath UTF8String];
//[[appViewController returnObject].mDatabasePath UTF8String];
if (sqlite3_open(dbPath, &mDiary) == SQLITE_OK)
NSLog(@"%@",mPriority.text);
NSString *insertSQL2=[NSString stringWithFormat:@"INSERT INTO TODO VALUES(\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")",mStartDate.text,mEndDate.text,mTaskName.text,mTaskDescription.text,mPriority.text];
NSLog(@"%@",insertSQL2);
const char *insert_stmt2 = [insertSQL2 UTF8String];
sqlite3_prepare_v2(mDiary, insert_stmt2,
-1, &statement, NULL);
NSLog(@"%@",mPriority.text);
if (sqlite3_step(statement) == SQLITE_DONE )
mStartDate.text=@"";
mEndDate.text=@"";
mTaskName.text=@"";
mTaskDescription.text=@"";
mPriority.text=@"";
else
NSLog(@"failed to add");
sqlite3_finalize(statement);
sqlite3_close(mDiary);
//这里是测试:
-(void)testAddTask
AddTaskViewController *obj;
obj = [[AddTaskViewController alloc]init];
obj.mTaskName.text = @"t1";
obj.mTaskDescription.text = @"t2";
obj.mStartDate.text = @"56987";
obj.mEndDate.text = @"55425";
obj.mPriority.text = @"3";
[obj addTask];
sqlite3_stmt *statement;
const char *dbpath = [obj.mDatabasePath UTF8String];
if (sqlite3_open(dbpath,&mDiaryTest)== SQLITE_OK)
NSString *selectSQL = [NSString stringWithFormat:@"SELECT * FROM TODO WHERE mTaskName = \"%@\"",obj.mTaskName.text];
const char *query_stmt = [selectSQL UTF8String];
if (sqlite3_prepare_v2(mDiaryTest,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
if(sqlite3_step(statement) == SQLITE_ROW)
testString = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
sqlite3_finalize(statement);
sqlite3_close(mDiaryTest);
NSLog(@"%@",testString);
STAssertEquals(testString,@"t2",@"should be equal");
我发现在目标依赖项中项目未选中。我检查了它并再次测试,结果出现以下错误:
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_AddTaskViewController", referenced from:
objc-class-ref in testAddTask.o
"_sqlite3_close", referenced from:
-[testAddTask testAddTask] in testAddTask.o
"_sqlite3_column_text", referenced from:
-[testAddTask testAddTask] in testAddTask.o
"_sqlite3_finalize", referenced from:
-[testAddTask testAddTask] in testAddTask.o
"_sqlite3_open", referenced from:
-[testAddTask testAddTask] in testAddTask.o
"_sqlite3_prepare_v2", referenced from:
-[testAddTask testAddTask] in testAddTask.o
"_sqlite3_step", referenced from:
-[testAddTask testAddTask] in testAddTask.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我知道这可能离题了,因为最初的问题完全不同,但是谁能告诉我为什么会出现这些错误?我已经添加了必要的框架。
【问题讨论】:
您的测试名称是否以test
开头? OCUnit 只运行以那个词开头的方法。
是的,他们确实从测试开始
【参考方案1】:
当您测试 UIKit
依赖的东西时,您想运行 Application Tests。确保您已设置 Bundle Loader
和 Test Host
构建设置(请参阅 documentation)。
此外,您还必须将 libsqlite3.0.dylib
链接到您的测试目标,因为您在单元测试中使用了 sqlite 函数。
【讨论】:
感谢您的回复,我按照此链接查找有关 Bundle loader 和测试主机设置的更多信息:twobitlabs.com/2011/06/… 嘿,你能修复你帖子中的链接吗?他们 404。以上是关于OCUnit 测试用例未运行的主要内容,如果未能解决你的问题,请参考以下文章