Search code examples
unit-testingxcode5kif

How to cause Xcode 5 unit tests to fail early?


I'm running SenTestKit unit tests in Xcode 5 (actually, they're integration tests with KIF 2, but it amounts to the same thing). These tests take a long time to run. Is there a way to have the entire test suite fail with the first test failure?


Solution

  • You can abort testing on the first SetTestKit test failure by calling [self raiseAfterFailure] at the start of each test case. However, this does not work for KIF 2.

    I ended up subclassing SenTestObserver like so:

    @interface TestObserver : SenTestObserver
    @end
    
    @implementation TestObserver
    
    + (void)load {
        [[NSUserDefaults standardUserDefaults] setValue:@"SenTestLog,TestObserver"
                                                 forKey:SenTestObserverClassKey];
    }
    
    + (void)testCaseDidFail:(NSNotification *)notification {
        // Fail fast
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            exit(1);
        });
    }
    
    @end
    

    Edited to add dispatch_after so that there's time to capture test output before terminating the test suite.