从 iOS 中的 UITests 读取 Info.plist?
Reading Info.plist from UITests in iOS?
是否可以在 UITesting 时从 UITests
文件夹文件中的 Info.plist
文件中读取?如果是这样,如何。
到目前为止我已经尝试过:
if let url = Bundle.main.url(forResource: "Info", withExtension: "plist"),
let myDict = NSDictionary(contentsOf: url) as? [String:Any] {
print(myDict)
}
不过,returns 一些 plist
这不是我在主项目文件夹中的事件。
有一个特殊的测试包 class 您需要阅读(或注入)。试试这个。将下面 Bundle(for:)
调用中的 OSTests
替换为您的 XCTestCase subclass.
的名称
func testExample() {
let testBundle = Bundle(for: OSTests.self)
if let url = testBundle.url(forResource: "Info", withExtension: "plist"),
let myDict = NSDictionary(contentsOf: url) as? [String:Any] {
print(myDict)
}
}
然后您需要确保将测试包复制到您的测试应用中。 Select 你的测试目标。在 Build Phases 选项卡中公开 Copy Bundle Resources 部分。单击 + 图标和 select 测试目标的 Info.plist 文件。
这是我当时的结果。
是否可以在 UITesting 时从 UITests
文件夹文件中的 Info.plist
文件中读取?如果是这样,如何。
到目前为止我已经尝试过:
if let url = Bundle.main.url(forResource: "Info", withExtension: "plist"),
let myDict = NSDictionary(contentsOf: url) as? [String:Any] {
print(myDict)
}
不过,returns 一些 plist
这不是我在主项目文件夹中的事件。
有一个特殊的测试包 class 您需要阅读(或注入)。试试这个。将下面 Bundle(for:)
调用中的 OSTests
替换为您的 XCTestCase subclass.
func testExample() {
let testBundle = Bundle(for: OSTests.self)
if let url = testBundle.url(forResource: "Info", withExtension: "plist"),
let myDict = NSDictionary(contentsOf: url) as? [String:Any] {
print(myDict)
}
}
然后您需要确保将测试包复制到您的测试应用中。 Select 你的测试目标。在 Build Phases 选项卡中公开 Copy Bundle Resources 部分。单击 + 图标和 select 测试目标的 Info.plist 文件。
这是我当时的结果。