在XCUITest中读取本地JSON文件

编程入门 行业动态 更新时间:2024-10-23 14:34:04
本文介绍了在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文档: developer.apple/swift/blog/?id=37

Also, I had a look at the following Apple documentation: developer.apple/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文件

本文发布于:2023-11-24 09:09:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624601.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   XCUITest   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!