Create a gist now

Instantly share code, notes, and snippets.

@neilco /CustomTestLog.m Secret
Created Apr 24, 2014

What would you like to do?
//
// CustomTestLog.m
//
// Created by Neil on 24/04/2014.
//
#import <XCTest/XCTest.h>
@interface CustomTestLog : XCTestObserver
@property (nonatomic, strong) NSFileHandle *logFileHandle;
@end
@implementation CustomTestLog
+ (void)load
{
[[NSUserDefaults standardUserDefaults] setValue:NSStringFromClass(self) forKey:XCTestObserverClassKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)startObserving
{
[super startObserving];
self.logFileHandle = [NSFileHandle fileHandleWithStandardError];
}
- (void)stopObserving
{
[[NSUserDefaults standardUserDefaults] setValue:NSStringFromClass(self.superclass) forKey:XCTestObserverClassKey];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.logFileHandle closeFile];
[super stopObserving];
}
- (void) testSuiteDidStart:(XCTestRun *) testRun
{
[self log:@"Test Suite '%@' started at %@\n", testRun.test.name, [NSDate date]];
}
- (void) testSuiteDidStop:(XCTestRun *) testRun
{
[self log:@"Test Suite '%@' finished at %@.\nExecuted %d tests, with %d failure (%d unexpected) in %.3f (%.3f) seconds\n",
testRun.test.name, [NSDate date], testRun.testCaseCount, testRun.totalFailureCount, testRun.unexpectedExceptionCount,
testRun.testDuration, testRun.totalDuration];
}
- (void) testCaseDidStart:(XCTestRun *) testRun
{
[self log:@"Test Case '%@' started.\n", testRun.test.name];
}
- (void) testCaseDidStop:(XCTestRun *) testRun
{
[self log:@"Test Case '%@' %@ (%.3f seconds).\n", testRun.test.name,
testRun.hasSucceeded ? @"passed" : @"failed",
testRun.testDuration];
}
- (void) testCaseDidFail:(XCTestRun *)testRun
withDescription:(NSString *)description
inFile:(NSString *)filePath
atLine:(NSUInteger)lineNumber
{
[self log:@"%@:%d: error: %@ : %@\n", filePath, lineNumber, testRun.test.name, description];
}
- (void)log:(NSString *)format, ...
{
va_list args;
va_start(args, format);
NSString *formattedString = [[NSString alloc] initWithFormat:format arguments:args];
[self.logFileHandle writeData:[formattedString dataUsingEncoding:NSUTF8StringEncoding]];
va_end(args);
}
@end
//
// FailingTestLog.m
//
// Created by Neil on 24/04/2014.
//
#import <XCTest/XCTest.h>
@interface FailingTestLog : XCTestObserver
@property (nonatomic, strong) NSFileHandle *logFileHandle;
@end
@implementation FailingTestLog
+ (void)load
{
[[NSUserDefaults standardUserDefaults] setValue:NSStringFromClass(self) forKey:XCTestObserverClassKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)startObserving
{
[super startObserving];
self.logFileHandle = [NSFileHandle fileHandleWithStandardError];
}
- (void)stopObserving
{
[[NSUserDefaults standardUserDefaults] setValue:NSStringFromClass(self.superclass) forKey:XCTestObserverClassKey];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.logFileHandle closeFile];
[super stopObserving];
}
- (void)testCaseDidStop:(XCTestRun *) testRun
{
[self log:@"Test Case '%@' %@ (%.3f seconds).\n", testRun.test.name,
testRun.hasSucceeded ? @"passed" : @"failed",
testRun.testDuration];
}
- (void)testCaseDidFail:(XCTestRun *)testRun
withDescription:(NSString *)description
inFile:(NSString *)filePath
atLine:(NSUInteger)lineNumber
{
[self log:@"%@:%d: error: %@ : %@\n", filePath, lineNumber, testRun.test.name, description];
}
- (void)log:(NSString *)format, ...
{
va_list args;
va_start(args, format);
NSString *formattedString = [[NSString alloc] initWithFormat:format arguments:args];
[self.logFileHandle writeData:[formattedString dataUsingEncoding:NSUTF8StringEncoding]];
va_end(args);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment