Skip to content

Commit

Permalink
fixing the way test execution was halted during a failure. Instead of…
Browse files Browse the repository at this point in the history
… rethrowing a custom exception, rely on xctest's ability to halt execution of the test case.
  • Loading branch information
khandpur committed Feb 26, 2016
1 parent ade8516 commit 4b5052a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
7 changes: 7 additions & 0 deletions EarlGrey/Additions/XCTestCase+GREYAdditions.h
Expand Up @@ -88,10 +88,17 @@ UIKIT_EXTERN NSString *const kGREYXCTestCaseNotificationKey;
*/
- (NSString *)grey_testClassName;

/**
* Interrupts the current test case execution immediately, tears down the test and marks it as
* failed.
*/
- (void)grey_interruptExecution;

/**
* @return A unique test outputs directory for the current test. All test related outputs should be
* under this directory (and subdirectories).
*/
- (NSString *)grey_localizedTestOutputsDirectory;

@end

27 changes: 21 additions & 6 deletions EarlGrey/Additions/XCTestCase+GREYAdditions.m
Expand Up @@ -20,6 +20,7 @@

#import "Common/GREYPrivate.h"
#import "Common/GREYSwizzler.h"
#import "Exception/GREYFrameworkException.h"
#import "Synchronization/GREYAppStateTracker.h"
#import "Synchronization/GREYUIThreadExecutor.h"

Expand Down Expand Up @@ -49,6 +50,11 @@
*/
static const void *const kTestCaseStatus = &kTestCaseStatus;

/**
* Name of the exception that's thrown to interrupt current test execution.
*/
static NSString *const kInternalTestInterruptException = @"EarlGreyInternalTestInterruptException";

/**
* Enumeration with the possible statuses of a XCTestCase.
*/
Expand Down Expand Up @@ -160,6 +166,11 @@ - (NSString *)grey_localizedTestOutputsDirectory {
return localizedTestOutputsDir;
}

- (void)grey_interruptExecution {
[[GREYFrameworkException exceptionWithName:kInternalTestInterruptException
reason:@"Immediately halt execution of testcase"] raise];
}

#pragma mark - Private

- (BOOL)grey_isSwizzled {
Expand Down Expand Up @@ -222,7 +233,6 @@ - (void)grey_invokeTest {
error:&error];

NSAssert(success, @"Failed to create localized outputs directory. Cause: %@", error);

INVOKE_ORIGINAL_IMP(void, @selector(grey_invokeTest));

// The test may have been marked as failed if a failure was recorded with the
Expand All @@ -233,7 +243,9 @@ - (void)grey_invokeTest {
}
} @catch(NSException *exception) {
[self grey_setStatus:kGREYXCTestCaseStatusFailed];
@throw;
if (![exception.name isEqualToString:kInternalTestInterruptException]) {
[exception raise];
}
} @finally {
switch ([self grey_status]) {
case kGREYXCTestCaseStatusFailed:
Expand Down Expand Up @@ -304,10 +316,12 @@ - (void)grey_recordFailureWithDescription:(NSString *)description
atLine:(NSUInteger)lineNumber
expected:(BOOL)expected {
[self grey_setStatus:kGREYXCTestCaseStatusFailed];
[self grey_recordFailureWithDescription:description
inFile:filePath
atLine:lineNumber
expected:expected];
INVOKE_ORIGINAL_IMP4(void,
@selector(grey_recordFailureWithDescription:inFile:atLine:expected:),
description,
filePath,
lineNumber,
expected);
}

/**
Expand Down Expand Up @@ -365,3 +379,4 @@ - (BOOL)grey_createDirRemovingExistingDir:(NSString *)path error:(NSError **)out
}

@end

9 changes: 5 additions & 4 deletions EarlGrey/Exception/GREYDefaultFailureHandler.m
Expand Up @@ -111,14 +111,14 @@ - (void)handleException:(GREYFrameworkException *)exception details:(NSString *)
} else {
failureDescription = [NSString stringWithFormat:@"%@ has occurred.", [exception class]];
}
NSLog(@"%@", exceptionLog);

[XCTestCase grey_currentTestCase].continueAfterFailure = NO;
[[XCTestCase grey_currentTestCase] recordFailureWithDescription:failureDescription
inFile:_fileName
atLine:_lineNumber
expected:NO];
NSLog(@"%@", exceptionLog);

// This will cause the current test case to stop executing further.
[GREYFrameworkException raise:exception.name format:@"%@", exception.reason];
[[XCTestCase grey_currentTestCase] grey_interruptExecution];
}

#pragma mark - Private
Expand Down Expand Up @@ -154,3 +154,4 @@ - (void)grey_savePNGImage:(UIImage *)image
}

@end

0 comments on commit 4b5052a

Please sign in to comment.