在XCUITest中读取本地JSON文件 [英] Read local JSON file in XCUITest

查看:208
本文介绍了在XCUITest中读取本地JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为iOS应用程序编写UI测试.我想从本地json文件读取数据.

I am writing UI tests for iOS application. I want to read data from a local json file.

我正在使用以下代码来获取我的json文件的路径:

I am using the following code to get the path for my json file:

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")
    }

我已尝试将解决方案用于以下stackoverflow问题: 使用Swift读取JSON文件.没用!

I have tried using the solution for the following stackoverflow question : Reading in a JSON File Using Swift. Didn't work!

我还查看了以下Apple文档: https://developer.apple.com/swift/blog/?id=37

Also, I had a look at the following Apple documentation: https://developer.apple.com/swift/blog/?id=37

任何帮助将不胜感激!

推荐答案

首先,您需要检查.json文件是否与测试文件位于同一目标中.如果文件在主要目标中,则必须使用Bundle.main.但是,如果它与您的测试位于同一目标中,请使用以下代码.

First of all, you need to check the .json file is in the same target with the test file. If the file is in the main target, you have to use Bundle.main. However if it is in the same target with your test, use the below code.

let t = type(of: self)
let bundle = Bundle(for: t.self)
let path = bundle.path(forResource: "myurl", ofType: "json")

这篇关于在XCUITest中读取本地JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆