首页 > 解决方案 > Xcode 单元测试 - 添加一个可在所有测试类中使用的通用函数

问题描述

我已经使用 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
    }
}

标签: iosswiftxcodeunit-testingxctest

解决方案


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

首先,将一个新的 swift 文件添加到您的测试项目中,最好命名为描述性的,在这种情况下CSVTestUtility或者CSVTestHelper是合理的。

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

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

如您所见,帮助程序和实用程序是帮助单元测试共享通用功能的宝贵工具,但与往常一样,请确保您的工作易于识别,这样您就可以了解未来的意图是什么,当您可能不那么新鲜时项目。


推荐阅读