首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在swift中加载TestCase文件

如何在swift中加载TestCase文件
EN

Stack Overflow用户
提问于 2020-07-22 14:48:54
回答 1查看 165关注 0票数 2

在我的swift项目的Tests目录中,我有一个用于存放测试用例的目录和一个用于存放测试文件的目录。

代码语言:javascript
复制
+ Tests
   + UnitTest
       + MySwiftTests.swift
   + SourceFiles
       + file1.xml

我需要创建一个FileManager来加载我的MySwiftTests中的'file1.xml‘。我的问题是如何在FileManager的url中指定与测试用例相关的SearchPathDirectory和SearchPathDomainMask?

函数URL (用于: FileManager.SearchPathDirectory,位于: FileManager.SearchPathDomainMask,appropriateFor: URL?,创建: Bool) -> URL

https://developer.apple.com/documentation/foundation/filemanager

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-22 15:50:05

代码语言:javascript
复制
let bundle = Bundle(for: type(of: self))
guard let path = bundle.path(forResource: "file1", ofType: "xml") else {
    // File not found ... oops
    return 
}
// Now you can access the file using e.g. String(contentsOfFile:)
let string = try? String(contentsOfFile: path)
// or Data? using the FileManager
let data = FileManager.default.contents(atPath: path)

确保将file1.xml添加到测试目标中

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63028452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档