json - 如何从 XCUITest 读取 Json 文件?

标签 json swift xcode xcuitest

我的 xcode 项目中有一个 json 文件,我正尝试通过以下方式读取该文件:

let fileUrl = Bundle.main.url(forResource: "myfile", withExtension: "json")

等等。

当我从项目目标成员中的文件(如 View Controller 文件)运行此代码时,代码运行并且我能够读取 json。但是,当在我的 XCUITestCase 文件中运行它时 – 它失败了,无法找到 url。

如何从 XCUItest 中读取 json 文件?

我看过这篇文章,但没有帮助 Read local JSON file in XCUITest

最佳答案

稍微修改一下你的代码,这对我有用

if let file = Bundle(for: type(of: self)).url(forResource: "YOUR_FILE", withExtension: "json") {
    let data = try! Data(contentsOf: file)
    let dataJson = try! JSON(data: data)
    // TODO Your handle here
} else {
    XCTFail("Cannot read json file")
}

关于json - 如何从 XCUITest 读取 Json 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57432822/

相关文章:

javascript - 按 Angular.js 中的公共(public)对象属性过滤数组?

python - json.loads() 不保持秩序

json - golang : Unmarshal: json: cannot unmarshal array into Go value of type main. MonitorServerInfo

json - Dart:具有 .fromJson 构造函数的泛型

ios - 做手势时将 anchor 放在屏幕中央

ios - 对讲机 : iOS App crashing on updating user only on iOS 10. 3.2

swift - 是否可以快速读取权利文件

XCode4 覆盖存储库离线

ios - 如何隐藏 Google map 位置点

ios - 应用程序安装失败-应用程序 bundle 不包含有效的标识符-Xcode8.2