Spectre

Build Status

Special Executive for Command-line Test Running and Execution (命令行测试运行和执行专用执行者).

一个用于 Swift 项目和 playground 的行为驱动开发 (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 目前有两个内置的报告器,Standard (标准) 和 Dot (点) 报告器。 支持自定义报告器,创建一个符合 Reporter 协议的类型即可。

可以通过环境变量配置默认报告器。 例如

$ env SPECTRE_REPORTER=dot swift test
$ env SPECTRE_REPORTER=tap swift test

Standard (标准)

标准报告器产生如下输出

通过测试

Standard Reporter Success

未通过测试

Standard Reporter Failure

Dot (点)

使用 -t 参数,您可以使用点报告器。

通过测试

Dot Reporter Success

未通过测试

Dot Reporter Failure

期望

等价性

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

安装 / 运行

Swift Package Manager

查看 Commander 作为示例。

Playground

您可以在 Xcode Playground 中使用 Spectre,在此仓库中打开 Spectre.playground,失败将打印在控制台中。

Spectre in an Xcode Playground