Test Anything Protocol (TAP) for Swift
- Add this project to your
Package.swift
.
import PackageDescription
let package = Package(
// ...
dependencies: [
.Package(
url: "https://github.com/dankogai/swift-tap.git", from: "0.6.0"
)
],
// ...
)
- write [main.swift] like below:
import TAP
let test = TAP(tests:3)
test.ok(42+0.195 == 42.195, "42 + 0.195 == 42.195") // ok 1 - 42 + 0.195 == 42.195
test.eq(42+0.195, 42.195, "42 + 0.195 is 42.195") // ok 2 - 42 + 0.195 is 42.195
test.ne(42+0.195, 42, "42 + 0.195 is not 42") // ok 3 - 42 + 0.195 is not 42
test.done()
- build and test like below:
$ swift run && prove .build/debug/main
./main .. ok
All tests successful.
Files=1, Tests=2, 0 wallclock secs ( 0.02 usr + 0.00 sys = 0.02 CPU)
Result: PASS
./main
actually prints the following:
1..3
ok 1 - 42 + 0.195 == 42.195
ok 2 - 42 + 0.195 is 42.195
ok 3 - 42 + 0.195 is not 42
You should specify the number of tests to run via TAP(tests:Int)
or .plan(_:Int)
but this is optional. But when you do so you cannot omit .done()
which prints 1..number_of_tests
that the TAP protocol demands.
ok 1 - 42 + 0.195 == 42.195
ok 2 - 42 + 0.195 is 42.195
ok 3 - 42 + 0.195 is not 42
1..2
Checks if predicate
is true.
func ok(@autoclosure predicate:()->Bool, _ message:String = "")->Bool
Checks if actual
== expected
. Analogous to .ok(actual == expected)
but it is more informative when it is not ok.
On the other hand both actual
and expected
must be Equatable
. So:
- use
.eq
whenever you can. - use
.ok
as a last resort.
public func eq<T:Equatable>(actual:T, _ expected:T, _ message:String = "")->Bool
As you can tell from the type signatures, .eq
and .ne
accepts arrays and dictionaries if they are equatable.
The opposite of .eq
.
public func ne<T:Equatable>(actual:T, _ expected:T, _ message:String = "")->Bool
Why NOT XCTest?
- Still way too fancy and resource-demanding for some occasions