DEV Community

Khoa Pham
Khoa Pham

Posted on

Testing keychain in iOS

Today I was upgrading Keychain to swift 4, and take this opportunity to fix the test. The tests pass on macOS, but on iOS, I get -25300 error for

var status = SecItemCopyMatching(query as CFDictionary, nil)
Enter fullscreen mode Exit fullscreen mode

It is because there is no Keychain entitlement for test target. But this is a framework, how can I add entitlement 🤔 The solution is to use a Test Host to host the XCTest tests. See my pull request

Create test host target

First create an iOS app to act as a test host

Enable Keychain capability

Then enable Keychain capability to let Xcode automatically create an entitlement file for you. Note that you can just enter the Keychain group. You don't need go to Apple Developer dashboard to configure anything

Specify Test Host

Then in you test target, specify Test Host by using $(BUILT_PRODUCTS_DIR)/TestHost_iOS.app/TestHost_iOS

Now run your test again, it should pass 🎉

Original post https://medium.com/fantageek/testing-keychain-in-ios-9360c1f38a28

Top comments (0)