感谢@stanislaw-pankevich 的精彩回答。在这里,为了完整起见,我将(或多或少)包括我最终完成的完整测试程序,其中包括一些额外的细节和 cmets。
(从我的角度来看,这是一个完整的程序,因为它测试功能
在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(这是主程序,makefile 实际调用它来运行所有测试):
#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;
}
生成文件:
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 $@ $< -framework Cocoa -F$(FRAMEWORKS) -rpath $(FRAMEWORKS) \
-framework XCTest $(TESTCASES:=.o) -L.. -lmylib
注意事项:
-
XCTestObserver 类现已弃用,取而代之的是 XCTestObservation。
- 测试结果被发送到一个 shared XCTestObservationCenter,不幸的是,它会分散注意力到 stderr(因此必须重定向到其他地方)——似乎不可能避免这种情况并将它们发送只到我的观察中心。在我的实际程序中,我将
runtests.m 中的 NSLog 调用替换为与标准输出聊天的函数,因此我可以将其与转到默认观察中心的聊天区分开来。
- 另见overview documentation
(假设您使用的是 XCode),
- ...XCTest API documentation,
- ...以及文件标题中的注释(例如)
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers