问题
In tests target -> General -> Testing: set Host Application to None
, so that no app gets launched.
But in that case I cannot use Bundle.main.resourcePath
and access resources of my main application (in which some command files are included as resources and I need to run them using Process()
).
Could anyone suggest a solution?
回答1:
Bundle(identifier: "com.something.app")
will get you the bundle for a specific bundle ID. The problem is that if your bundle ID changes for your main target this will fail.
You can also try getting the bundle for a specific class:
let bundle = Bundle(for: SomeClass.self)
Where SomeClass
is a class in your main bundle.
回答2:
Added file to 'Copy Bundle Resources' of Test target also, then used following code to get path for resource , instead of "\(Bundle.main.resourcePath!)/test.command"
let testBundle = Bundle(for: type(of: self))
let path = "\(testBundle.resourcePath!)/test.command""
来源:https://stackoverflow.com/questions/48301370/accessing-bundle-of-main-application-while-running-xctests