謝謝,@ stanislaw-pankevich,一個很好的答案。在這裏,爲了完整起見,我包括了(或多或少)完成的測試程序,其中包括一些額外的細節和評論。
(這是從我的角度來看一個完整的程序,因爲它在測試定義util.h
功能 ,這是不包括在內)
文件UtilTest.h
:
#import <XCTest/XCTest.h>
@interface UtilTest : XCTestCase
@end
文件UtilTest.m
:
#import "UtilTest.h"
#import "../util.h" // the definition of the functions being tested
@implementation UtilTest
// We could add methods setUp and tearDown here.
// Every no-arg method which starts test... is included as a test-case.
- (void)testPathCanonicalization
{
XCTAssertEqualObjects(canonicalisePath("/p1/./p2///p3/..//f3"), @"/p1/p2/f3");
}
@end
驅動程序runtests.m
(這是主程序,它是小時生成文件實際上調用運行所有測試):
#import "UtilTest.h"
#import <XCTest/XCTestObservationCenter.h>
// Define my Observation object -- I only have to do this in one place
@interface BrownieTestObservation : NSObject<XCTestObservation>
@property (assign, nonatomic) NSUInteger testsFailed;
@property (assign, nonatomic) NSUInteger testsCalled;
@end
@implementation BrownieTestObservation
- (instancetype)init {
self = [super init];
self.testsFailed = 0;
return self;
}
// We can add various other functions here, to be informed about
// various events: see XCTestObservation at
// https://developer.apple.com/reference/xctest?language=objc
- (void)testSuiteWillStart:(XCTestSuite *)testSuite {
NSLog(@"suite %@...", [testSuite name]);
self.testsCalled = 0;
}
- (void)testSuiteDidFinish:(XCTestSuite *)testSuite {
NSLog(@"...suite %@ (%tu tests)", [testSuite name], self.testsCalled);
}
- (void)testCaseWillStart:(XCTestSuite *)testCase {
NSLog(@" test case: %@", [testCase name]);
self.testsCalled++;
}
- (void)testCase:(XCTestCase *)testCase didFailWithDescription:(NSString *)description inFile:(NSString *)filePath atLine:(NSUInteger)lineNumber {
NSLog(@" FAILED: %@, %@ (%@:%tu)", testCase, description, filePath, lineNumber);
self.testsFailed++;
}
@end
int main(int argc, char** argv) {
XCTestObservationCenter *center = [XCTestObservationCenter sharedTestObservationCenter];
BrownieTestObservation *observer = [BrownieTestObservation new];
[center addTestObserver:observer];
Class classes[] = { [UtilTest class], }; // add other classes here
int nclasses = sizeof(classes)/sizeof(classes[0]);
for (int i=0; i<nclasses; i++) {
XCTestSuite *suite = [XCTestSuite testSuiteForTestCaseClass:classes[i]];
[suite runTest];
}
int rval = 0;
if (observer.testsFailed > 0) {
NSLog(@"runtests: %tu failures", observer.testsFailed);
rval = 1;
}
return rval;
}
的Makefile:
FRAMEWORKS=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks
TESTCASES=UtilTest
%.o: %.m
clang -F$(FRAMEWORKS) -c $<
check: runtests
./runtests 2>runtests.stderr
runtests: runtests.o $(TESTCASES:=.o) ../libmylib.a
cc -o [email protected] $< -framework Cocoa -F$(FRAMEWORKS) -rpath $(FRAMEWORKS) \
-framework XCTest $(TESTCASES:=.o) -L.. -lmylib
注:
- 的
XCTestObserver
類現在已經過時,並通過XCTestObservation
取代。
- 的測試結果發送到共享 XCTestObservationCenter,不幸的是發狂似的喋喋不休到stderr(因此必須被重定向到其他地方) - 它似乎並沒有能夠避免,並讓他們發送只我觀察中心。在我的實際程序中,我用
runtests.m
中的NSLog
調用替換了一個聊天者到stdout的函數,因此我可以從聊天記錄中區分出默認的ObservationCenter。
- 又見overview documentation (假設您正在使用的XCode),
- ...的XCTest API documentation,
- ...,並在(例如)在文件的報頭中的注意事項
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers
Thanks @ stanislaw-pankevich - 看起來非常有用。我仍然在通過頭文件中的文檔和文檔工作,但可能會返回問題。有了這個指針,我也發現[XCTest文檔](https://developer.apple.com/reference/xctest),並且正在抓我的腦袋...... –