Disabling exception breakpoint when running unit tests in Xcode
By: Brian Webster |
I just figured out how to solve a problem in Xcode that’s vexed me for a number of years, and thought I’d share. The problem is that I usually have Xcode’s built-in “All Exceptions” breakpoint enabled when debugging my application, but when I run unit tests in Xcode, I have certain unit tests that will throw exceptions and trigger the breakpoint, halting the test until I tell it to continue. So far my only solutions have been to babysit the unit test while it runs and click “Continue” when it halts, or disable the “All Exceptions” breakpoint, which I inevitably forget to turn back on when I go back to debugging my app normally. (This is a test bundle that runs with a host application, so the whole app gets launched when running the unit tests)
The solution I’ve found goes like this. First, I wrote a function in my app that will tell me whether I’m in a unit testing environment. The best method I’ve found for this from Stack Overflow looks like this:
func BWIsUnitTesting() -> Bool {
return NSProcessInfo.processInfo().environment["XCTestConfigurationFilePath"] != nil
}
Then, open up the breakpoints navigator in Xcode, right-click on the exceptions breakpoint, and add !BWIsUnitTesting() to the “Condition” field, like this:
Voila! Now when you run your app normally, Xcode will break on exceptions, but when you are running unit tests, it won’t.