Capriccio

Swift 5.0 Build Status codecov

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")
    }
}

使用 Capriccio 有什么好处?

Gherkin feature 文件可以轻松地在不同平台之间共享。借助 Capriccio,您可以从特定文件夹中的 feature 文件生成可执行代码,您只需在构建过程中运行 Capriccio 即可(例如在脚本阶段)。

运行时 vs 编译时

有很多不同的工具允许从 feature 文件运行测试,例如 CumberishXCTest-Gherkin。但是它们在运行时生成测试。通过在编译时生成测试,您可以获得一些好处

如何使用它

要使用它,只需运行

capriccio source destination <option>

source 包含 feature 文件的文件夹路径 destination 将生成 swift 文件的文件夹路径

命令行选项

配置文件

除了 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>

自定义 setUp 和 tearDown

您的 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 模板上使用哪些属性。