onmyway133 / blog

🍁 What you don't know is what you haven't learned
https://onmyway133.com/
MIT License
669 stars 33 forks source link

Testing keychain in iOS #92

Open onmyway133 opened 6 years ago

onmyway133 commented 6 years ago

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, you can name it TestHost_iOS

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

keychain

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 🎉

SaidAlSouti commented 2 months ago

How to get the actual path of the testhost for multiproject system