一个原生的、轻量级的 Swift/XCTest BDD 测试库。
CutwormBDD 将高级的 Gherkin 特性规格与测试连接起来。
通过插入到测试中的类似注解的函数调用来添加 BDD 功能。 运行测试时,Cutworm 验证测试是否满足 Gherkin 场景并记录结果。
与 Cucumber 框架不同,Cutworm 不需要将测试拆分为独立的步骤,也不控制测试执行。 因此,改编现有测试很容易,并且 Cutworm 可以很好地与 Xcode 配合使用。
要了解 Cutworm 的工作原理,请在 Xcode 中打开 CutwormBDDDemo 项目并运行测试。
首先,在您的测试目标中创建一个名为 features
的目录,其中包含 Gherkin 功能定义。
features/authentication.feature
Feature: Authentication
As a user, I want to be able to reliably log into my account
Scenario: Email login with valid credentials
Given the user has an account
When the user attempts to log in with their credentials
Then the user is successfully logged in
对于 Swift 包,测试目标应该设置为依赖 CutwormBDD,并将 features 放在 resources 中
.testTarget(
name: "MyTests",
dependencies: ["CutwormBDD"],
resources: [.copy("features")]
),
接下来,为某个特性创建一个测试用例,并运行一次测试以生成 BDD 模板
class AuthenticationTests: XCTestCase, BDDTestCase {
override func setUp() {
super.setUp()
// Informs Cutworm which feature is under test
Feature("Authentication", in: .module)
}
func testLogin() throws {
// When executed, this replaces the body of `testLogin` with a BDD template.
GenerateScenario_EXPERIMENTAL("Email login with valid credentials")
}
}
现在模板已经生成,您可以编写您的测试,将测试的各个部分分解为步骤
class AuthenticationTests: XCTestCase, BDDTestCase {
override func setUp() {
super.setUp()
// Informs Cutworm which feature is under test
Feature("Authentication", in: .module)
}
func testLogin() throws {
Scenario("Email login with valid credentials")
Given("the user has an account")
// If an assertion failure occurs, or uncaught error is thrown during a step,
// the step is failed and the scenario is consider failed.
let user = try makeTestUser()
When("the user attempts to log in with their credentials")
loginScreen.typeCredentials(username: user.username, password: user.password)
let result = loginScreen.submit()
Then("the user is successfully logged in")
XCTAssertEqual(result, .success)
}
}
最后,运行测试。 您将在控制台中看到 BDD 结果的打印输出,并将其导出到 JSON 文件
Exporting BDD results for MyTests...
Feature: Authentication
Scenario: Email login with valid credentials
Steps:
✅ given the user has an account (0.00s)
✅ when the user attempts to log in with their credentials (0.00s)
✅ then the user is successfully logged in (0.00s)
Writing results to JSON file...
BDD result JSON is saved at path ...json
如果第二步失败,结果将如下所示
Feature: Authentication
Scenario: Email login with valid credentials
Steps:
✅ given the user has an account (0.00s)
❌ when the user attempts to log in with their credentials (0.04s)
⏩ then the user is successfully logged in (0.00s)
我们始终感谢社区的贡献。 要更改项目,您可以克隆 repo 并在 Xcode 中打开该文件夹。 完成更改后,您可以运行测试以确保一切正常,并提交 pull request。
查看 未解决的问题,以获取建议的功能(和已知问题)列表。
Scenario Outline
、Examples
、Background
、Rule
和标签。