佩斯特有一个有趣的秘密特征,我们需要谈论。它支持Gherkin式的功能规格。我不知道这甚至是一件事,直到我看着Joel Bennett的模块,看到我以前没有看到的东西。

此功能允许您以简单的业务可读语法定义您的功能和规范。然后,您将使用该规范执行的验证脚本箱。它将让您在每个物品上给您通过/失败结果,如纠正。我认为这很棒,更多的人需要了解它。我不确定介绍这个功能,所以你可能需要更新纠缠才能得到它。

这是3件系列的第一个帖子在gherkin上。

  • 第1部分:基本的gherkin介绍(这篇文章)
  • 第2部分: 先进的长柄特征
  • 第3部分:使用Gherkin(计划但未发布)

指数

什么是gherkin?

嫩香 是用于创建规范的特定业务可读域名语言。老实说,我对此不太了解,但我发现了这些主题的这些引用。

即使我不了解它的起源,我仍然可以向你展示它在纠缠/陈旧中的工作原理。

我们的第一个规格

We need to start by creating a specification. These plain text files need to be saved with a .feature extension for them to get automatically processed.

Feature: You can copy one file

Scenario: The file exists, and the target folder exists
    Given we have a source file
    And we have a destination folder
    When we call Copy-Item
    Then we have a new file in the destination
    And the new file is the same as the original file

Save this as .\copyfile.feature and now we have a Gherkin-style specification. If this was part of a module, I would save it into a spec folder.

邀请丛林

Now we need to execute 邀请丛林 like we would Invoke-Pester. Here is the output from our sample.

 吉尔金特色仅限

因为这些颜色很难读取,所以这是原始文本。

PS > Invoke-Gherkin
Testing all features in '.\spec'

Feature: You can copy one file

Scenario: The file exists, and the target folder exists
    [!] Given we have a source file 15ms
    [!] And we have a destination folder 2ms
    [!] When we call Copy-Item 3ms
    [!] Then we have a new file in the destination 8ms
    [!] And the new file is the same as the original file 5ms

Testing completed in 483ms
Scenarios Passed: 0 Failed: 0
Steps Passed: 0 Failed: 0 Skipped: 5 Pending: 0 Inconclusive: 0

这将枚举我们拥有的所有功能规范。并运行任何相应的测试。我们没有创建任何测试,所以他们都没有通过。

添加测试步骤

The set of tests for a Gherkin feature are called steps. Because of that they need saved in a file with a name ending in .Steps.ps1. Add these steps to a new file and save it as CopyItem.Steps.ps1.

Given 'we have a source file' {
    mkdir testdrive:\source -ErrorAction SilentlyContinue
    Set-Content 'testdrive:\source\something.txt' -Value 'Data'
    'testdrive:\source\something.txt' | Should Exist
}

Given 'we have a destination folder' {
    mkdir testdrive:\target -ErrorAction SilentlyContinue
    'testdrive:\target' | Should Exist
}

When 'we call Copy-Item' {
    { Copy-Item testdrive:\source\something.txt testdrive:\target } | Should Not Throw
}

Then 'we have a new file in the destination' {
    'testdrive:\target\something.txt' | Should Exist
}

Then 'the new file is the same as the original file' {
    $primary = Get-FileHash testdrive:\target\something.txt
    $secondary = Get-FileHash testdrive:\source\something.txt
    $secondary.Hash | Should Be $primary.Hash
}

You will notice that I created a Pester-style test for several lines in the specification. Each one starts with a keyword of Given, And, When orThen. (But is also a valid keyword). The description is pulled directly from the specification. The 邀请丛林 uses that description to make the match.

I also want to point out that the step keywords (Given, And, When, Then, But) are interchangeable within the code. Use Given to set up for or prep for an action, When for the actions, and Then for validation. The And and But keywords are there to make the specification flow better and will match to any Given,When or Then test.

运行测试步骤

Now when we run the 邀请丛林 , we get this output.

 gherkin特征通过

PS> Invoke-Gherkin
Testing all features in '.\spec'

Feature: You can copy one file

Scenario: The file exists, and the target folder exists
    [+] Given we have a source file 15ms
    [+] And we have a destination folder 2ms
    [+] When we call Copy-Item 3ms
    [+] Then we have a new file in the destination 8ms
    [+] And the new file is the same as the original file 5ms
Testing completed in 35ms
Scenarios Passed: 0 Failed: 0
Steps Passed: 5 Failed: 0 Skipped: 0 Pending: 0 Inconclusive: 0

在这里,我们看到所有的测试都通过,因为我们拥有所有这些功能。

其他详情

您可以在同一文件中具有多种情况。我只使用了一个场景来保持示例简单。

您也不必在步骤文件和功能文件之间有1到1次映射​​。我在这个例子中确实如此让这篇文章更容易写入。如果可以使用多个功能文件和一个步骤文件,更容易使用。重要的是它们内部的字符串彼此映射。

只要文本是完全匹配,不同的规格就可以具有线映射到同一测试。当您有具有相同依赖性或大量重叠步骤的情况时,这很好。

为什么不使用invoke-pester?

佩斯特很擅长很多东西,但它是在一所思想中建造的。 Gherkin从不同的前瞻性接近问题。这只是我们使用的另一个工具。

下一步是什么?

我们只是看看了最基本的例子。我们只划伤了表面。在Gherkin的下一篇文章中,我将覆盖其余特征。