阅读XCUITest中的本地JSON文件
我正在为iOS应用程序编写UI测试。我想从本地json文件读取数据。阅读XCUITest中的本地JSON文件
我用下面的代码获取的路径,我的JSON文件: Reading in a JSON File Using Swift:
func testExample() {
readJSON()
}
func readJSON(){
let bundle = Bundle(for:myTestClass.self)
if let path = bundle.url(forResource: "myurl", withExtension: "json"){
print("Got the path")
print(path)
}
else{
print("Invalid filename/path")
}
我曾尝试使用以下计算器问题的解决尝试。没有工作!
另外,我看了一下下面的Apple文档: https://developer.apple.com/swift/blog/?id=37
任何帮助将不胜感激!
这不是你如何从Bundle
的iOS
中读取本地json文件。它以下列方式进行:
// "myUrl" is assumed to be your json file name
if let pathStr: String = Bundle.main.path(forResource: "myurl", ofType: ".json") {
print("json path: \(pathStr)")
}
这正是我们如何阅读UI Test Bundle中的本地json文件。 –
@KushalJogi,有什么问题?它不起作用吗?我可以请知道 – KrishnaCA
这不会工作,因为主包不是你认为的那个,它是测试跑步者的包,我猜你没有那个JSON那里 –
首先,你需要检查.json
文件与测试文件相同的目标。如果该文件是主要目标,你必须使用Bundle.main
,但它是在您的测试相同的目标,
let t = type(of: self)
let bundle = Bundle(for: t.self)
let path = bundle.path(forResource: "myurl", ofType: "json")
被存储在本地目录中的JSON或它重视的Xcode? – KrishnaCA