I\'m trying to use a resource file in unit tests and access it with Bundle.path
, but it returns nil.
This call in MyProjectTests.swift returns nil:
Currently, swift package manager (SPM) doesn't handle resources, here is an issue open in SPM's bug tracking system https://bugs.swift.org/browse/SR-2866.
Until support for resources is implemented in SPM, I would copy resources to the locations where the result executable expects resources to be during runtime. You can know these locations by printing Bundle.resourcePath
property. I would perform this copying automatically, using a Makefile for example. This way Makefile becomes a "build orchestrator" on top of SPM.
I wrote an example to demonstrate how this approach works on MacOS and Linux -https://github.com/vadimeisenbergibm/SwiftResourceHandlingExample.
The user would run make commands: make build
and make test
instead of swift build
and swift test
. Make will copy the resources to the expected locations (different on MacOS and Linux, during run and during tests).