验收标准驱动开发提供了简单的类型来构建面向场景的测试 (given, when, then),以便在 Swift 中更容易地遵循行为驱动开发。
https://github.com/ptrkstr/ACDD-Swift
Add to target
设置为你的测试目标import ACDD
func test_success() throws {
var calculator = SumCalculator()
try AC(scenario: "Calculator should be able to sum")
.given("4 is entered into the calculator") {
calculator.firstNumber = 4
}
.and("5 is entered into the calculator") {
calculator.secondNumber = 5
}
.when("the sum is computed") {
try calculator.computeSum()
}
.then("the output is 9") {
XCTAssertEqual(calculator.output!, 9)
}
}
日志记录通过 LoggerType
协议处理。你可以通过遵循此协议并使用以下方法之一链接它来创建自己的日志记录器。此软件包附带一个 SimpleLogger
,它像这样打印到控制台
AC - Calculator should be able to sum
- GIVEN 4 is entered into the calculator
- AND 5 is entered into the calculator
- WHEN the sum is computed
- THEN the output is 9
使用以下代码为所有 AC
设置相同的日志记录器
ACDD.defaultLoger = SimpleLogger()
这最好放在任何测试运行之前的位置。此 SO 帖子中的建议。
使用以下代码为每个 AC
设置一个日志记录器
AC(scenario: "The world should not end", logger: HadronColliderLogger())
ACDD 强制执行顺序。
given
, when
, then
。and
。每个代码处理程序都是可选的,因为并不总是需要编写代码。一个例子可能是
func test_no_input() throws {
var calculator = SumCalculator()
try AC()
.given("no numbers have been entered into the calculator")
.when("the sum is computed") {
XCTAssertThrowsError(try calculator.computeSum())
}
.then("an error occurs")
}
允许省略 given
。这在将 BDD 应用于 UI 开发时很有帮助。例如
func test_text_exists() throws {
var contentView: ContentView!
try AC()
.when("the screen appears") {
contentView = ContentView()
}
.then("I can see `Hello, world!`") {
let inspection = try contentView.inspect()
XCTAssertNoThrow(try inspection.find(text: "Hello, world!"))
}
}
上述代码使用 ViewInspector
每个子句函数都会重新抛出处理程序的错误,以消除在测试中填充冗长的 do {} catch {}
的需要。如果你想选择加入此功能,请添加
AC
前面添加 try
throws