Giter Club home page Giter Club logo

spek's Introduction

Spek

โค๏ธ Support my app โค๏ธ

โค๏ธโค๏ธ๐Ÿ˜‡๐Ÿ˜๐Ÿค˜โค๏ธโค๏ธ

Spek is a lightweight function builder style Behavior Driven Development framework, designed to be used with XCTest

Usage

โš ๏ธBefore using any test frameworks, you need to ensure there is XCTest framework in your test target.

import XCTest
@testable import Spek

final class SpekTests: XCTestCase {
    func testExample() {
        var left = 1
        var right = 1

        spec {
            Describe("math") {
                BeforeEach {
                    left = 2
                }

                Describe("basic") {
                    BeforeEach {
                        right = 3
                    }

                    AfterEach {

                    }

                    It("adds correctly") {
                        XCTAssertEqual(left + right, 5)
                    }

                    It("multiplies correctly") {
                        XCTAssertEqual(left * right, 6)
                    }
                }
            }
        }
    }
}

Features

  • Support common BDD parts: Describe, Context, BeforeAll, AfterAll, BeforeEach, AfterEach, It
  • To disable a part, prefix it with X, for example XDescribe, XIt, ...
  • All blocks are throwing function, so are encouraged to do try catch
  • BeforeAll and AfterAll are run before and after all enclosing Describe and It
  • BeforeEach and AfterEach are run before and after all enclosing It
  • Context behaves the same as Describe

How to

Disable a block

When you have an imcomplete block, instead of commenting it out, you can prefix it with X so it won't be executed

Describe("math") {
    It("will be run") {

    }

    XIt("won't be run") {

    }
}

Declare local variables

Spek uses Swift 5.1 function builder so you can only declare block and nested block. To declare local variables in a subspec, you need to use Sub and return Describe explicitly

Describe("math") {
    Describe("advanced") {

    }

    Sub {
        let abc = 1
        let def = 2
        return Describe("extra") {
            It("should add") {
                XCTAssertEqual(abc + def, 2)
            }
        }
    }
}

Generate XCTestCase

By default, spec method runs the test directly, to generate test cases, you need to override makeDescribe.

Subclass SpekTestCase and override makeDescribe method, Spek will convert your Describe descriptions and generate XCTestCase methods. It generates test methods for nested Describe and Sub too.

For example the below test will generate test_math_should_work and test_math_advanced_should_calculate methods

import XCTest
import Spek

class GenerateTestCaseTests: SpekTestCase {
    override class func describe() -> Describe {
        Describe("math") {
            It("should work") {
                XCTAssertTrue(1 + 1 == 2)
            }

            Describe("advanced") {
                It("should calculate") {
                    XCTAssertEqual(2 * 5, 10)
                }
            }
        }
    }
}

Install

Spek is distributed using the Swift Package Manager. To install it into a project, add it as a dependency within your Package.swift manifest:

let package = Package(
    ...
    dependencies: [
        .package(url: "https://github.com/onmyway133/Spek.git", from: "0.5.0")
    ],
    ...
)

Then import Spek in your XCTest

import Spek

Author

Khoa Pham, [email protected]

Credit

  • Quick for testInvocations reference

License

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

spek's People

Contributors

onmyway133 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

spek's Issues

Undefined symbol error adding Spek with SPM

Hi after adding Spek with SPM in a clean project with Xcode 12.4 I get this error in Build time:

Undefined symbol: OBJC_METACLASS$_XCTestCase

Captura de pantalla 2021-04-09 a las 20 10 02

Is there anything else that I must do to make it compile? thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.