Spectre
Special Executive for Command-line Test Running and Execution.
Spectre 是一个适用于 Swift 项目和工作区的行为驱动开发(BDD)框架和测试运行器。它与 OS X 和 Linux 都兼容。
使用方法
describe("a person") {
let person = Person(name: "Kyle")
$0.it("has a name") {
try expect(person.name) == "Kyle"
}
$0.it("returns the name as description") {
try expect(person.description) == "Kyle"
}
}报告
Spectre 目前有两个内置的报告器,标准报告器和点报告器。支持自定义报告器,只需创建一个符合 Reporter 的类型。
- 标准报告器
- 点报告器 (
-t) - 敲击报告器 (
--tap)- 兼容Test Anything Protocol 的输出
标准报告器
标准报告器会产生如下输出:
通过测试
失败测试
点
使用 -t 参数,您可以使用 dot 报告器。
通过测试
失败测试
期望
等价性
try expect(name) == "Kyle"
try expect(name) != "Kyle"真值
try expect(alive).to.beTrue()
try expect(alive).to.beFalse()
try expect(alive).to.beNil()错误处理
try expect(try write()).toThrow()
try expect(try write()).toThrow(FileError.NoPermission)可比较的
try expect(5) > 2
try expect(5) >= 2
try expect(5) < 10
try expect(5) <= 10类型
try expect("kyle").to.beOfType(String.self)导致失败
throw failure("Everything is broken.")自定义断言
您可以轻松提供自己的断言,只需要在断言不满足预期时抛出失败。
示例
以下项目使用 Spectre
| 项目 | CI |
|---|---|
| Stencil | |
| Inquiline | |
| CardKit | N/A |
安装/运行
SPM
请查看 spectre-build.
Conche
Conche 构建系统的对 Spectre 支持。您只需将您的 Conche podspec 中的 test_spec 依赖于 Spectre,它就会使用 conche test 运行您的测试。
沙箱
您可以在 Xcode 沙箱中使用 Spectre,在本存储库中打开 Spectre.playground,失败信息会打印在控制台中。
手动
您可以将 Spectre 构建为框架或库并与之链接。
例如,如果您克隆 Spectre 并运行 make,它将构建一个可以链接的库
$ swiftc -I .conche/modules -L .conche/lib -lSpectre -o runner myTests.swift
$ ./runner



