SenTesting stop test after first STAssert fails

669 Views Asked by At

Is there a way to stop execution of an iOS unit test once the first STAssert fails?

For example if I have multiple STAsserts:

STAssertTrue([myobject succeeded], @"failed");
STAssertNotNil(foo,@"bar");

I would love it if Xcode simply stopped executing the test after the first one fails. Any way to do this?

3

There are 3 best solutions below

0
On

If you have an Exception Breakpoint set for all exceptions, Xcode will throw to the debugger on the first failed test. If you're running tests manually within Xcode this might be acceptable.

To setup an exception breakpoint within Xcode 4+

  1. View > Navigators > Show Breakpoint Navigator
  2. Click the + button at bottom left of the Breakpoint Navigator window, choose "Add Exception Breakpoint"
  3. Click "Done" in the popover that appears.

Xcode will break to the debugger on any exceptions encountered at application runtime.

0
On

If you add the following method to your test class any failing STAssert statement will stop the execution of the test method.

- (void)failWithException:(NSException *)anException{
  [super failWithException:anException];
  NSAssert(false, @"An assertion has failed");
}
0
On

SenTestCase has a method -[SenTestCase raiseAfterFailure] which causes STAssert... to throw an exception on completion, preventing the next line in the test from executing.

You can do this on a test-by-test basis:

- (void)testSomeStuff
{
    [self raiseAfterFailure];
    STAssertTrue([myobject succeeded], @"failed");
    STAssertNotNil(foo,@"bar");
}

Or at a class level:

- (void)setUp
{
    [super setUp];
    [self raiseAfterFailure];
}