swift - 代码。添加到测试目标的图像资源不会复制到测试包中

标签 swift xcode unit-testing

我为 UIImage 类编写了一些扩展,并希望通过单元测试覆盖它。我向测试目标添加了一些图像,并检查它是否出现在 Copy Bundle Resources 列表中。 Copy Bundle Resources

在测试代码中,我使用了一个为我提供测试数据的模型对象。

class TestConstants {
    private static var bundle: Bundle {
        return Bundle(for: UIImageExtensionsTests.self)
    }
    private static var birdImageURL: URL {
        let path = self.bundle.url(forResource: "birds", withExtension: "png")
        return path!
    }
    static var birdImageData: Data {
        return try! Data(contentsOf: self.birdImageURL)
    }
}

不幸的是,birds.png 图像不在测试包中,但存在另一个资源 drm.txtenter image description here enter image description here 我有点困惑这是 Xcode 中的错误吗? 顺便说一下,我下载了 Xcode 9.4 beta 并且有相同的行为 - 图像不会复制到测试包中。

enter image description here

#更新:

UIImageExtensionsTests 是一个测试类,与 drm.txtbirds.png 文件在同一目标中呈现

enter image description here enter image description here

最佳答案

要获得适当的 Bundle,请使用:

 let bundle = Bundle.init(for: TestConstants.classForCoder())

关于swift - 代码。添加到测试目标的图像资源不会复制到测试包中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49794276/

相关文章:

ios - Swift 中的透明 UINavigationBar

ios - 带有数组字符串的子字符串 - Swift

xcode - 如何在Xcode 7中使用代码覆盖率?

objective-c - [self view] 和 self.view 的区别?

unit-testing - 在Go lang中一个接一个地多次模拟具有不同响应的相同功能

javascript - 使用 Jasmine 访问 js 模块的方法,以便可以单独测试它们

ios - 在 tvOS 上禁用 AVPlayer 下拉菜单

swift - 使用 CALayer 和 UIImage 在 Swift 中屏蔽图像

ios - 无法找到任何至少支持 iOS 8.0 的 Xcode 安装

c++ - Google 在裸机 stm32 MCU 上的测试