Tablier

Build Status codecov Swift 5 CocoaPods compatible Carthage compatible SPM compatible Supports iOS, macOS, tvOS and Linux MIT License

一个用于 表格驱动测试 的微框架。

A screenshot to see how it works

特性

安装

Swift Package Manager

.package(url: "https://github.com/akkyie/Tablier", from: <#version#>)

Cocoapods

target 'YourTests' do
    inherit! :search_paths
    pod 'Tablier'
end

用法

同步配方

您可以定义一个测试配方来测试您的类、结构体或函数。

final class MyParseTests: XCTestCase {
    func testMyParse() {
        let recipe = Recipe<String, Int>(sync: { input in
            // `myParse` here is what you want to test
            let output: Int = try myParse(input) // it fails if an error is thrown
            return output
        })
...

然后您可以列出配方的输入和预期输出,以运行实际的测试。

...
        recipe.assert(with: self) {
            $0.when("1").expect(1)
            $0.when("1234567890").expect(1234567890)
            $0.when("-0x42").expect(-0x42)
        }
    }
}

异步配方

也支持定义带有异步完成处理程序的配方。

let recipe = Recipe<String, Int>(async: { input, complete in
    myComplexAndSlowParse(input) { (result: Int?, error: Error?) in
        complete(result, error)
    }
})

自 Swift 5.5 起,您可以使用 AsyncRecipe 定义带有 async/await 语法的异步配方

let recipe = AsyncRecipe<String, Int> { input in
    try await myComplexAndSlowParse(input)
}

注意

注意

目前,如果在同步初始化器中抛出错误,或者使用错误调用完成处理程序,则认为测试用例失败。将来会支持测试错误。

示例

许可证

MIT。 请参阅 LICENSE。