Testing XPC services
Xcode test bundles are actually executables that may embed everything a regular app can. This allows testing of embedded XPC services without employing any shenanigans:
- Add new Unit Test target
- Add the Copy Files phase to the created target with “XPC Services” as a destination; drag’n’drop your XPC service there
- Optionally add another Copy Files phase with “Frameworks” as a destination for every custom framework the service depends on; make sure that the Copy on Sign is checked if your XPC service is sandboxed and/or hardened
- Open the Schemes editor via Product → Schemes → Manage Schemes…
- Delete Unit Test’s scheme
- Select XPC service’s scheme and click Edit…
- Select the Test step, navigate to Info and add the Unit Test
To communicate with the XPC service from within tests set up NSXPCConnection
as usual and employ help of XCTestExpectation
for synchronization.