Thanks, @stanislaw-pankevich, for a great answer. Here, for completeness, I'm including (more-or-less) the complete test program which I ended up with, which includes a couple of extra details and comments.
(This is a complete program from my point of view, since it tests functions
defined in util.h
, which isn't included here)
File UtilTest.h
:
#import <XCTest/XCTest.h>
@interface UtilTest : XCTestCase
@end
File 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
Driver program runtests.m
(this is the main program, which the makefile actually invokes to run all the tests):
#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 $@ $< -framework Cocoa -F$(FRAMEWORKS) -rpath $(FRAMEWORKS) \
-framework XCTest $(TESTCASES:=.o) -L.. -lmylib
Notes:
- The
XCTestObserver
class is now deprecated, and replaced by XCTestObservation
.
- The results of tests are sent to a shared XCTestObservationCenter, which unfortunately chatters distractingly to stderr (which therefore has to be redirected elsewhere) – it doesn't seem possible to avoid that and have them sent only to my observation centre instead. In my actual program, I replaced the
NSLog
calls in runtests.m
with a function which chatters to stdout, which I could therefore distinguish from the chatter going to the default ObservationCenter.
- See also the overview documentation
(presumes that you're using XCode),
- ...the XCTest API documentation,
- ...and the notes in the headers of the files at (eg)
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers