All Projects → YusukeHosonuma → SwiftParamTest

YusukeHosonuma / SwiftParamTest

Licence: MIT license
Parameterized test for Swift

Programming Languages

swift
15916 projects
Makefile
30231 projects
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to SwiftParamTest

Pkg Ok
👌 Checks paths and scripts defined in package.json before you publish
Stars: ✭ 219 (+291.07%)
Mutual labels:  test
Alsatian
TypeScript testing framework with test cases
Stars: ✭ 244 (+335.71%)
Mutual labels:  test
Starwars-clean
Simple project with clean architecture
Stars: ✭ 34 (-39.29%)
Mutual labels:  test
Redux Form Test
Shows how to do unit tests and integration tests with Redux-Form
Stars: ✭ 221 (+294.64%)
Mutual labels:  test
Nightwatch Cucumber
[DEPRECATED] Cucumber.js plugin for Nightwatch.js.
Stars: ✭ 243 (+333.93%)
Mutual labels:  test
Vuex Mock Store
✅Simple and straightforward Vuex Store mock for vue-test-utils
Stars: ✭ 246 (+339.29%)
Mutual labels:  test
Nx Admin
👍 A magical 🐮 ⚔ vue admin,记得star
Stars: ✭ 2,497 (+4358.93%)
Mutual labels:  test
7182
Curso 7182 - Refatorando para testes de unidade
Stars: ✭ 21 (-62.5%)
Mutual labels:  test
Jest Html Reporters
🌈 Reporter for jest test framework. 🌈
Stars: ✭ 245 (+337.5%)
Mutual labels:  test
testza
Full-featured test framework for Go! Assertions, fuzzing, input testing, output capturing, and much more! 🍕
Stars: ✭ 409 (+630.36%)
Mutual labels:  test
Junit Dataprovider
A TestNG like dataprovider runner for JUnit with many additional features
Stars: ✭ 226 (+303.57%)
Mutual labels:  test
Jest Expect Message
Add custom message to Jest expects 🃏🗯
Stars: ✭ 240 (+328.57%)
Mutual labels:  test
Opentest
Open source test automation tool for web applications, mobile apps and APIs
Stars: ✭ 246 (+339.29%)
Mutual labels:  test
Fsharp Hedgehog
Release with confidence, state-of-the-art property testing for .NET.
Stars: ✭ 219 (+291.07%)
Mutual labels:  test
api-with-express
🌈 API with Express
Stars: ✭ 25 (-55.36%)
Mutual labels:  test
Foundry
A model factory library for creating expressive, auto-completable, on-demand dev/test fixtures with Symfony and Doctrine.
Stars: ✭ 216 (+285.71%)
Mutual labels:  test
Fluentdocker
Commands, Services and Fluent API for docker, docker-compose & docker-machine, for win/mac/linux and native docker in c#
Stars: ✭ 245 (+337.5%)
Mutual labels:  test
m.test
m(icro)test is a lightweight test runner for node.js written in es6+
Stars: ✭ 17 (-69.64%)
Mutual labels:  test
Tablier
A micro-framework for Table Driven Tests.
Stars: ✭ 33 (-41.07%)
Mutual labels:  test
Betterer
betterer makes it easier to make incremental improvements to your codebase
Stars: ✭ 248 (+342.86%)
Mutual labels:  test

SwiftParamTest

Test CocoaPods Carthage Compatible SPM Compatible License Twitter

Logo

Parameterized-test for Swift. (with XCTest)

Screenshot

Code Style

SwiftParamTest supports two way of code-style dependent on Swift version.

Result builders API (recommended)

I recommend this API when you use Swift 5.1 or later.

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

Legacy API

You can use array literal based API that like follwing when you use under Swift 5.1.

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),
])

Operator based API

You can specify row by use the operator ==> that like following:

// 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),
])

Save or Output Markdown Table

Save or output markdown table when enabled by option (default is all disable).

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

You can also specify column header name too.

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

You can also retrive markdown table from result too.

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 |

This is useful for copy and paste the test specification table to PR or others.

markdown table in PR

Xcode Code Snippets

Xcode Code Snippets

Copy .codesnippet files to the following directory from .xcode directory:

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

and restart Xcode.

Or run the following command from the root of the repository:

$ make snippets

Example

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

Custom Assertion

SwiftParamTest uses XCTAssertEqual() and owns error messages by default.

But you can use custom assertion like follows.

// 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
    // ----
    //
}

Limitation

  • Only up to four arguments are supported.

FAQ

Can't compile or compiler is clashed

This library use many type inference, therefore type inference are failed in sometime. This may be resolved by explicitly specifying the type information.

for example:

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

Installation

CocoaPods

pod 'SwiftParamTest'

Swift Package Manager

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

Carthage

Write following to Cartfile.private.

github "YusukeHosonuma/SwiftParamTest"

Author

Yusuke Hosonuma / [email protected]

License

SwiftParamTest is available under the MIT license. See the LICENSE file for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].