Observe is a BDD / TDD test framework for Swift 4 that pairs very nicely with the Focus assertion framework.
Observe and Focus are not coupled to any other frameworks so you can include this package in your tests or your main app code.
- Swift 4.0+
Open your Package.Swift
file and add the following dependency:
dependencies: [
.package(url: "https://github.com/ObserveSocial/Observe.git", from: "0.4.0")
]
Run swift build
in terminal to fetch this new dependency.
- Create a new XCTest file
import Observe
import XCTest
import Observe
class SimpleTest: XCTestCase {
func testSpec() {
describe("Person") {
context("Initialize a new Person") {
var person: Person!
beforeEach {
person(name: "Sam")
}
it("should be called sam") {
XCTAssertEqual(person.name, "Sam")
}
}
}
}
}
describe("") {}
context("") {}
it("") {}
given("") {}
when("") {}
then("") {}
and("") {}
beforeEach {}
The reporter defines how output is logged to the console.
Observe defines a Reportable
protocol that you can implement in order to customize how test output is logged. By default, Observe uses it's own very simple reporter. There is also a Clean Reporter which we recommend you use.
If you want to build your own reporter, simply create a new type and have it conform to the Reportable
protocol. Then tell Observe to use your custom Reporter.
import XCTest
import Observe
class MyReporter: Observe.Reportable {
let sharedInstance = MyReporter()
func willRunBlock(file: StaticString, method: String, line: UInt, message: String, blockType: BlockType, indentationLevel: Int) {
print(message)
}
func didRunBlock(file: StaticString, method: String, line: UInt, message: String, blockType: BlockType, indentationLevel: Int) {
}
}
class SimpleTest: XCTestCase {
override class func setUp() {
super.setUp()
let reporter = MyReporter.sharedInstance
Observe.set(reporter: reporter)
}
func testSpec() {
describe("Person") {
...
}
}
}
class AnotherSimpleTest: XCTestCase {
override class func setUp() {
super.setUp()
let reporter = MyReporter.sharedInstance
Observe.set(reporter: reporter)
}
...
}
It can be useful to have your reporter be a singleton so that you can share state across multiple tests. This will allow you to keep track of stats like how many tests were executed.
Describe application behaviour rather than verifying code.
Observe is not coupled to XCTest at all, in fact Observe will never rely on any frameworks other than Foundation
.
This means you never have to worry about importing any other frameworks into your code. It also means that you can use this framework in your application's tests or your application's core code.
All developers should feel welcome and encouraged to contribute to Observe, see our CONTRIBUTING document here to get involved.