ios - Xcode 单元测试 - 添加一个通用函数以在所有测试类中使用

标签 ios swift xcode unit-testing xctest

我已经使用 Swift 为我的项目实现了单元测试。在测试用例中,我读取的输入值是来自 CSV 文件的结果并验证它们。目前,每当我创建一个新的测试用例类时,读取 CSV 文件并解析它的所有函数都需要复制并粘贴到新的测试类中。有没有办法使用这些函数在一个地方读取 CSV 文件,以便所有测试类都可以使用它们?我想重用的代码是:

func csv(data: String) -> [[String]] {
    var result: [[String]] = []
    let rows = data.components(separatedBy: "\n")
    for row in rows {
        let columns = row.components(separatedBy: ",")
        result.append(columns)
    }
    return result
}

func cleanRows(file:String)->String{
    var cleanFile = file
    cleanFile = cleanFile.replacingOccurrences(of: "\r", with: "\n")
    cleanFile = cleanFile.replacingOccurrences(of: "\n\n", with: "\n")
    return cleanFile
}

func readDataFromCSV(fileName:String, fileType: String)-> String!{
    let bundle = Bundle(for: type(of: self))
    let path = bundle.path(forResource: fileName, ofType: fileType)!

    do {
        let contents = try String(contentsOfFile: path, encoding: .utf8)
        return contents
    } catch {
        print("File Read Error for file \(path)")
        return nil
    }
}

最佳答案

您可以使用帮助文件来处理这个问题。您可以使用静态函数和扩展来完成此操作。

首先,向您的测试项目添加一个新的 swift 文件,最好命名为具有描述性的名称,在这种情况下,CSVTestUtilityCSVTestHelper 是合理的。

接下来我们要创建一个包含这些方法的结构。

struct CSVTestUtility {
    static func csv(data: String) -> [[String]] {
        var result: [[String]] = []
        let rows = data.components(separatedBy: "\n")
        for row in rows {
            let columns = row.components(separatedBy: ",")
            result.append(columns)
        }
        return result
    }

    static func cleanRows(file:String)->String{
        var cleanFile = file
        cleanFile = cleanFile.replacingOccurrences(of: "\r", with: "\n")
        cleanFile = cleanFile.replacingOccurrences(of: "\n\n", with: "\n")
        return cleanFile
    }

    static func readDataFromCSV(fileName:String, fileType: String)-> String!{
        let bundle = Bundle(for: type(of: self) as! AnyClass)
        let path = bundle.path(forResource: fileName, ofType: fileType)!

        do {
            let contents = try String(contentsOfFile: path, encoding: .utf8)
            return contents
        } catch {
            print("File Read Error for file \(path)")
            return nil
        }
    }

此时我们要确保该文件已添加到单元测试项目中,因此请确保为测试项目的文件设置了目标成员资格。一旦完成,我们就可以通过静态结构调用来调用这些方法。

有一点需要注意,Bundle init 可能会根据您希望调用它的方式提供一些意想不到的功能。如果您计划在测试用例之外测试其他包,则可能需要对其进行更改。

同样值得注意的一件事是,其中两个方法采用字符串输入,因此可以将它们重构为字符串扩展。

extension String {
    func csv() -> [[String]] {
        var result: [[String]] = []
        let rows = self.components(separatedBy: "\n")
        for row in rows {
            let columns = row.components(separatedBy: ",")
            result.append(columns)
        }
        return result
    }

    func cleanRows()->String{
        var cleanFile = self
        cleanFile = cleanFile.replacingOccurrences(of: "\r", with: "\n")
        cleanFile = cleanFile.replacingOccurrences(of: "\n\n", with: "\n")
        return cleanFile
    }
}

因此,如果您要将上述扩展名放入新的 CSVTestUtility 文件中,您将能够直接从正在使用的字符串中访问这些方法,例如:

var csvData = "somedata"
var csvConvertedData = csvData.csv()
for row in csvConvertedData {
    row.cleanRows()
}

如您所见,帮助程序和实用程序是帮助单元测试共享通用功能的重要工具,但一如既往,请确保您的工作易于识别,以便您在将来可能不那么清楚时了解其意图是什么新鲜的项目。

关于ios - Xcode 单元测试 - 添加一个通用函数以在所有测试类中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51822718/

相关文章:

ios - 解析查询,图像数组返回空

ios - 在 iPhone 的 iOS 7 中打开相机时横向模式下的方向问题

ios - 无法在 UITableView Swift 中隐藏空单元格

html - Swift - 解析网页

xcode - 从新的 Firebase(数据库)中检索数据

ios - 在 XCode 中找不到应用程序证明功能

android - GLSL - 用于图像处理/混合的 fragment 着色器

ios - 无法实例化名为 SCNView 的类

ios - AFNetworking - NSMutableDictionary 参数

ios - 在 Angular 6 微应用中选取图像后,ImagePicker 关闭到黑屏