Accessing Bundle of main application while running XCTests

南笙酒味 提交于 2021-01-28 11:11:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!