Capriccio 是一个从 Gherkin .feature
文件生成 UI 测试的工具。
Capriccio 默认使用 XCTest-Gherkin 生成测试文件,但也可以使用您自己的 Stencil 模板 :)
如果您有如下 feature 文件
Feature: Feature number one
Scenario: Scenario I want to test
Given I'm in a situation
When something happens
Then something else happens
Scenario: Other scenario I want to test
Given I'm in another situation
When something different happens
Then something else happens
它会生成
import XCTest
import XCTest_Gherkin
final class FeatureNumberOne: XCTestCase {
func testScenarioIWantToTest() {
Given("I'm in a situation")
When("Something happens")
Then("Something else happens")
}
func testOtherScenarioIWantToTest() {
Given("I'm in another situation")
When("Something different happens")
Then("Something else happens")
}
}
Gherkin feature 文件可以轻松地在不同平台之间共享。借助 Capriccio,您可以从特定文件夹中的 feature 文件生成可执行代码,您只需在构建过程中运行 Capriccio 即可(例如在脚本阶段)。
有很多不同的工具允许从 feature 文件运行测试,例如 Cumberish 或 XCTest-Gherkin。但是它们在运行时生成测试。通过在编译时生成测试,您可以获得一些好处
要使用它,只需运行
capriccio source destination <option>
source 包含 feature 文件的文件夹路径 destination 将生成 swift 文件的文件夹路径
--excluded-tags
- 以逗号分隔的排除标签列表--included-tags
- 以逗号分隔的包含标签列表--class-type
[默认值: XCTestCase] - 生成的类的类类型--single-file
- 生成一个包含所有 feature 文件内容的单个 swift 文件--disable-swiflint
- 在文件中禁用 swiftlint--template-file
- Stencil 模板文件的路径除了 CLI 参数,您还可以使用 .capriccio.yml
配置文件
source: <source path>
output: <destination path>
template: <template path>
excludedTags:
- <string value>
- <string value>
includedTags:
- <string value>
- <string value>
classType: <string value>
singleFile: <bool value>
disableSwiftLint: <bool value>
您的 UI 测试可能需要在每次测试之前和之后执行一些特定于您的代码库的操作。为了使 Capriccio 能够支持所有这些需求,您可以使用 -c
或 --class-type
选项。这允许您创建一个通用类,您可以将其用作所有生成的类的超类。
例如:
class GherkinTestCase: XCTestCase {
var mockedServer: MockedServer
var stepDefinition: StepDefinitions!
var application: App!
override func setUp() {
super.setUp()
mockedServer = MockedServer()
mockedServer.start()
stepDefinition = StepDefinitions(testCase: self)
application = App()
application.launch()
}
override func tearDown() {
mockedServer.stop()
application.terminate()
super.tearDown()
}
}
然后,如果您运行
capriccio source destination -c GherkinTestCase
所有生成的类都将是 GherkinTestCase
的子类,而不是 XCTestCase
的子类
Capriccio 为每个示例创建一个不同的测试。
例如:
Feature: Feature number one
Scenario Outline: Scenario I want to test
Given I'm in a situation
When something happens <key1>
Then something else happens <key2>
Examples:
| key1 | key2 |
| value1 | value2 |
| value3 | value4 |
生成
import XCTest
import XCTest_Gherkin
final class FeatureNumberOne: XCTestCase {
func testScenarioIWantToTestWithValue1AndValue2() {
Given("I'm in a situation")
When("Something happens value1")
Then("Something else happens value2")
}
func testScenarioIWantToTestWithValue3AndValue4() {
Given("I'm in a situation")
When("Something happens value3")
Then("Something else happens value4")
}
}
查看我们的 模型类,了解您可以在 Stencil 模板上使用哪些属性。