Setup Host App for Keychain Tests in Xcode 8

When it comes to test keychain stuff in Xcode 8 / iOS 10, 💥

OSStatus: The operation couldn’t be completed. (OSStatus error -34018.)

In human readable words, there is an error when a required entitlement isn’t present.

😤

The problem is caused by the fact that XCTests are run by an executable within Xcode 8 and that process does not have entitlements. However, keychain stuff requires entitlements in order to be working properly. Understandable, but very inconvenient for testing my code that requires Keychain.

A workaround is adding a dummy app target that is set up with Keychain Sharing in the entitlements, and then use this app as a host app for running the tests.

Step 1:

Turn on the Keychain Sharing in the host app target’s Capabilities tab, and set up Keychain Groups accordingly

Step 2:

Select the host app in the Host Application

Now, ⌘+U on the test target, Xcode should launches the simulator, and then execute the tests without the 34018 error.

The downside of this approach is that, in order to complete the tests, simulator has to be launched. It is also fair to say that long-term technical debt could be running the tests eventually for projects that use Swift Package Manager instead of Xcode.