【问题标题】:Accessing Bundle of main application while running XCTests在运行 XCTests 时访问主应用程序包
【发布时间】:2018-01-17 12:38:54
【问题描述】:

在测试中target -> General -> Testing: set Host Application to None,因此不会启动任何应用。

但在这种情况下,我无法使用 Bundle.main.resourcePath 并访问我的主应用程序的资源(其中包含一些命令文件作为资源,我需要使用 Process() 运行它们)。

有人可以提出解决方案吗?

【问题讨论】:

    标签: swift bundle xctest nstask


    【解决方案1】:

    Bundle(identifier: "com.something.app") 将为您获取特定捆绑包 ID 的捆绑包。问题是,如果您的主要目标的捆绑 ID 发生更改,这将失败。

    您也可以尝试获取特定类的捆绑包:

    let bundle = Bundle(for: SomeClass.self)

    SomeClass 是主包中的一个类。

    【讨论】:

    • 我曾尝试过 'Bundle(identifier: mainappbundleid)',但是在运行测试目标时,这给出了 nil,而 'Bundle(for: SomeClass.self)' 给出了测试目标本身的包。
    • 我还是卡住了。运行测试目标时,是否会加载主应用程序包(因为主机应用程序设置为无)?可能这就是为什么 Bundle(identifier) 给出 nil 的原因?
    【解决方案2】:

    还将文件添加到 Test target 的 'Copy Bundle Resources',然后使用以下代码获取资源的路径,而不是 "\(Bundle.main.resourcePath!)/test.command"

    let testBundle = Bundle(for: type(of: self)) let path = "\(testBundle.resourcePath!)/test.command""

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 2023-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      • 1970-01-01
      相关资源
      最近更新 更多