DEV Community

Khoa Pham
Khoa Pham

Posted on

Testing keychain in iOS

Original post https://github.com/onmyway133/blog/issues/92

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)

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

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

target

Specify Test Host

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

test host

Now run your test again, it should pass 🎉

Top comments (1)

Collapse
 
hlungx profile image
Hlung

Thanks for the info bro!