Xcode 单元测试 - 添加要在所有测试中使用的通用函数 类

Xcode Unit Testing - Add a common function to be used across all test classes

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

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

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