Host Application for SPM Tests

Couldn't find anything on this, but is there anyway to run swift test inside of a host application? Attempting to test a package that has keychain interactions.

3 Likes

Not sure exactly what the restrictions are you need to get around, but if calling through Process is allowed, this package has a method which would make the experiment a one‐liner. If it works, you can reverse‐engineer from there. (On the other hand, if using Process is a no‐go, then even statically linking the package manager itself probably would not work, since it uses Process internally to communicate with swiftc and xctest.)

Or do you mean applying the tests to a running application like Xcode does when you tell it to unit test an application target?

My previous answer assumed you only meant you needed to trigger the tests from an application of your own.

Testing keychain in iOS · Issue #92 · onmyway133/blog · GitHub describes the problem a bit more succinctly. This solves the issue in Xcode (looks like this goes back to Xcode 8 Make Unit Test suite work on Xcode 8 · Issue #93 · square/Valet · GitHub), but I'm not sure of a way to fix the issue when running tests at the CLI with the Swift Package Manager.

Then you mean you want to unit test an application, not a framework.

That cannot be done from the package manager because there is no such thing as an application target.

For now, doing that requires Xcode.

Maybe mocking the keychain would be an alternate solution to the underlying problem?

It's a bit of both; like square/Valet, it's a framework to wrap functionality in the Keychain, but it must run within an application. Figured this would be the case with the package manager.

Were you ever able to find a solution to this? I'm running across the issue now myself, for Keychain testing as well.

2 Likes

+1 bumping... Has anyone found a good workaround for this? Perhaps this could be a SPM related proposal...

1 Like

What's the reason that you can't run your tests against Keychain directly? Do you need entitlements or..?

I think i'm facing the same issue. I'm new to Swift Package Manager and trying to convert an cocoapod I wrote, into a swift package. Part of this package caches data on disk and encrypts it with the secure enclave.

When I moved over the XCTests from my cocoapod, the ones using the encrypted cache are failing, because generating a key is returning an error:

UserInfo={numberOfErrorsDeep=0, NSDescription=Key generation failed, error -34018}

Which OSStatus says is "Missing entitlements". I think I need a host application in order to fix that but there doesn't seem to be any ability to do that. Has anyone found a work around for this?

1 Like

Yes, using certain keychain APIs require entitlements and I don't believe there is a way to pass entitlements into a test target.

1 Like

You could make a wrapper application that just has a picker for which tests to run, a button to run that test, a label to output the result and then have simple UI tests run each of them.

Unfortunately, as mentioned by David Harris here : How can test targets be run with entitlement? - #4 by davidharris, at this time you have no other option than creating pbxproj with empty host application to run tests that use Keychain ...

I am facing exactly the same issue, but I cannot find a way to host my package's tests in an iOS app. Is there anyone who managed to do this?

1 Like

i have the same problem, if someone find a solution ?

1 Like

Hallo, I‘m facing the same issue.. is there already some kind of solution or a workaround?

1 Like