Does anybody know if this has to do with restrictions of the UI testing bundle … ?
It does. UI test bundles are, as the name suggests, bundles, not standalone apps. These bundles are actually executed by a test runner app, currently called ***-Runner.app, where *** is the test target name. When you run a test, Xcode copies an internal template it has for this runner, embeds your test bundle into that, and then runs it.
The problem here is that this runner does not have any entitlements, and specifically it does not have the
com.apple.security.network.server
entitlement, and thus can’t listen for incoming connections [1].
The solution is to add that entitlement to your UI test bundle [2]. Normally you add entitlements via Xcode’s shiny Capabilities editor, but this is not enabled for a UI test bundle [3]. Rather, you must manually create a
.entitlements
file and then reference it in the Code Signing Entitlements (
CODE_SIGN_ENTITLEMENTS
) build setting of the UI test bundle target. Xcode will then merge those entitlements in to the test runner as part of the build process.
I tried this out here in my office (using Xcode 10.1) and it seems to work a treat.
Two final things to note:
Feel free to file an enhancement request against Xcode for a better way to set this up. Manually editing
.entitlements
file is not fun.Please post your bug number, just for the record.
The above is all about UI test bundles. Unit test bundles are a different *****. I’ve talked about this elsewhere on DevForums [4], but I’m calling it out here to head off any confusion between the two cases.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
[1] Curiously, the default test runner setup does include the
com.apple.security.network.client
entitlement.
[2] Strictly speaking, entitlements can only be applied to apps and app extensions. Bundles do not have entitlements, but rather inherit their entitlements from the process which loads them. What’s actually going on here is that Xcode has a special case where it applies the UI test bundle’s entitlements to the test runner as part of the build process.
[3] This is understandable given the previous footnote, but it’s still bugworthy (-:
[4] For example, this post. Be aware that the specific issue covered in that thread was resolved a while back, but the general suggestions in that post still apply.