Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/onmyway133/spek
π Function builder BDD testing framework in Swift
https://github.com/onmyway133/spek
bdd builder describe function swift test
Last synced: about 2 months ago
JSON representation
π Function builder BDD testing framework in Swift
- Host: GitHub
- URL: https://github.com/onmyway133/spek
- Owner: onmyway133
- License: other
- Created: 2020-01-10T06:57:46.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-03-28T20:58:51.000Z (over 4 years ago)
- Last Synced: 2024-10-25T06:02:24.266Z (2 months ago)
- Topics: bdd, builder, describe, function, swift, test
- Language: Swift
- Homepage: https://onmyway133.com/apps/
- Size: 2.53 MB
- Stars: 123
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# Spek
β€οΈ Support my app β€οΈ
- [Push Hero - pure Swift native macOS application to test push notifications](https://www.producthunt.com/posts/push-hero-2)
- [PastePal - Pasteboard, note and shortcut manager](https://www.producthunt.com/posts/pastepal)
- [Frame recorder - Recorder gif and video with frame](https://www.producthunt.com/posts/frame-recorder)
- [Other apps](https://onmyway133.github.io/projects/)β€οΈβ€οΈπππ€β€οΈβ€οΈ
![](Screenshots/logo.png)
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.
```swift
import XCTest
@testable import Spekfinal class SpekTests: XCTestCase {
func testExample() {
var left = 1
var right = 1spec {
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
```swift
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
```swift
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.
![](Screenshots/s1.png)
For example the below test will generate `test_math_should_work` and `test_math_advanced_should_calculate` methods
```swift
import XCTest
import Spekclass 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:
```swift
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](https://github.com/Quick/Quick) for testInvocations reference
## License
**Spek** is available under the MIT license. See the [LICENSE](https://github.com/onmyway133/Spek/blob/master/LICENSE.md) file for more info.