SwiftParamTest 2.2.0

SwiftParamTest 2.2.0

Hosonuma Yusuke维护。



SwiftParamTest

Test CocoaPods Carthage Compatible SPM Compatible License Twitter

Logo

Swift的参数化测试(基于XCTest)

Screenshot

代码风格

SwiftParamTest根据Swift版本支持两种代码风格。

函数构建器API(推荐)

当您使用Swift 5.1或更高版本时,我推荐使用此API(因为该API使用函数构建器)。

assert(to: max) {
    args(1, 2, expect: 2)
    args(2, 1, expect: 2)
    args(4, 4, expect: 4)
}

// You can also use tuple (with label).

assert(to: max) {
    args((x: 1, y: 2), expect: 2)
    args((x: 2, y: 1), expect: 2)
    args((x: 4, y: 4), expect: 4)
}

旧版API

当您使用Swift 5.1以下版本时,您可以使用类似于以下的基于数组的API。

assert(to: max, expect: [
    args(1, 2, expect: 2),
    args(2, 1, expect: 2),
    args(4, 4, expect: 4),
])

// You can also use tuple (with label).

assert(to: max, expect: [
    args((x: 1, y: 2), expect: 2),
    args((x: 2, y: 1), expect: 2),
    args((x: 4, y: 4), expect: 4),
])

操作符基于API

您可以使用==>操作符指定行,如下所示:

// Function Builder API
assert(to: max) {
    expect((1, 2) ==> 2)
    expect((2, 1) ==> 2)
    expect((4, 4) ==> 4)
}

// Legacy API
assert(to: max, expect: [
    expect((1, 2) ==> 2),
    expect((2, 1) ==> 2),
    expect((4, 4) ==> 4),
])

保存或输出Markdown表格

当启用了选项(默认为全部禁用)时,可以保存或输出Markdown表格。

override func setUp() {
    ParameterizedTest.option = ParameterizedTest.Option(
        traceTable: .markdown,            // output console is enabled
        saveTableToAttachement: .markdown // save to attachement is enabled
    )
}

override func tearDown() {
    ParameterizedTest.option = ParameterizedTest.defaultOption // restore to default
}

func testExample() {
    assert(to: max) {
        args(1, 2, expect: 2)
        args(2, 1, expect: 2)
        args(4, 4, expect: 4)
    }
    // =>
    // | Args 0 | Args 1 | Expected |
    // |--------|--------|----------|
    // |      1 |      2 |        2 |
    // |      2 |      1 |        2 |
    // |      4 |      4 |        4 |
}

您还可以指定列头名称。

func testMarkdownTable() {
    assert(to: max, header: ["x", "y"]) { // specify `header`
        args(1, 2, expect: 2)
        args(2, 1, expect: 2)
        args(4, 4, expect: 4)
    }
    // =>
    // | x | y | Expected |
    // |---|---|----------|
    // | 1 | 2 |        2 |
    // | 2 | 1 |        2 |
    // | 4 | 4 |        4 |
}

markdown table

您还可以从结果中检索Markdown表格。

let tableString = assert(to: max, header: ["x", "y"]) {
                      args(1, 2, expect: 2)
                      args(2, 1, expect: 2)
                      args(4, 4, expect: 4)
                  }.table

print(tableString)
// =>
// | x | y | Expected |
// |---|---|----------|
// | 1 | 2 |        2 |
// | 2 | 1 |        2 |
// | 4 | 4 |        4 |

这对于将测试规范表格复制粘贴到PR或其他地方非常有用。

markdown table in PR

Xcode 代码片段

Xcode Code Snippets

.codesnippet 文件从 .xcode 目录复制到以下目录

~/Library/Developer/Xcode/UserData/CodeSnippets/

并重新启动Xcode。

或者从仓库根目录运行以下命令

$ make snippets

示例

import SwiftParamTest
import XCTest

class ExampleTests: XCTestCase {
    override func setUp() {
        ParameterizedTest.option = ParameterizedTest.Option(traceTable: .markdown,
                                                            saveTableToAttachement: .markdown)
    }

    override func tearDown() {
        ParameterizedTest.option = ParameterizedTest.defaultOption
    }

    func testExample() {
        // for `function`
        assert(to: abs) {
            args( 0, expect: 0)
            args( 2, expect: 2)
            args(-2, expect: 2)
        }

        // for `operator`
        assert(to: +) {
            args(1, 1, expect: 2)
            args(1, 2, expect: 3)
            args(2, 2, expect: 4)
        }

        // for `instance method` (when receiver is not fixed)
        assert(to: String.hasPrefix) {
            args("hello", "he", expect: true)
            args("hello", "HE", expect: false)
        }

        // for `instance method` (when receiver is fixed)
        assert(to: "hello".hasPrefix) {
            args("he", expect: true)
            args("HE", expect: false)
        }
    }
}

自定义断言

SwiftParamTest默认使用XCTAssertEqual()并拥有错误信息。

但您可以使用如下自定义断言。

// custom assertion
func customAssert<T: Equatable>(_ actual: T, _ expected: T, file: StaticString, line: UInt) {
    let message = """

    ----
    Expected: \(expected)
    Actual: \(actual)
    ----
    """
    XCTAssert(expected == actual, message, file: file, line: line)
}

// passed by `with` arguments
assert(to: fizzBuzz, with: customAssertion) {
    args(1, expect: "Fizz")
    // =>
    //
    // XCTAssertTrue failed -
    // ----
    // Expected: 1
    // Actual: Fizz
    // ----
    //
}

限制

  • 仅支持最多四个参数。

常见问题解答

无法编译或编译器冲突

这个库使用了大量类型推断,因此有时类型推断会失败。可以通过显式指定类型信息来解决。

例如:

// Legacy API
assert(to: max, expect: [
    args(1, 2, expect: 2),
    args(2, 1, expect: 2),
    args(4, 4, expect: 4),
] as [Row2<Int, Int, Int>]) // `N` in `RowN` is arguments count

// Function Builder API
typealias T = Int
assert(to: max) {
    args(1 as T, 2 as T, expect: 2)
    args(2 as T, 1 as T, expect: 2)
    args(4 as T, 4 as T, expect: 4)
}

安装

CocoaPods

pod 'SwiftParamTest'

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/YusukeHosonuma/SwiftParamTest.git", from: "2.2.0"),
]

Carthage

将以下内容写入 Cartfile.private

github "YusukeHosonuma/SwiftParamTest"

作者

细onzuki Hosonuma / [email protected]

许可

SwiftParamTest 采用 MIT 许可。更多信息请参阅 licenses 文件。