首页 > 解决方案 > Gherkin,如何编写一个具有可选给定步骤的场景?

问题描述

我正在编写一个简单的添加项目场景。该项目具有必填字段(名称、编号、日期),该项目还具有可选字段(类别、描述)。我如何指定字段:

scenario: add item
   given name
   and  number
   and  date
   but category is not null
   and description is no null
   then save item

这是正确的吗?

标签: syntaxgherkin

解决方案


我会将其拆分为多个场景,因为每个场景都应该测试一件事。

此外,您应该始终有一个 When 步骤。Given 是一个先决条件,并不总是需要,When 是一个动作, Then 是该动作的预期结果。如果没有 When 步骤,您是在说您有什么都不做的预期结果?

我会将功能文件编写为如下所示:

Feature: Add Item
    As a stock control manager
    I want to be able to add items to an inventory
    So that I have a catalogue of al items in stock

Business Rules:
    - Name, number and date are mandatory data
    - category and description are optional

Sceanrio: Add item witout category
    When I add an item without a category
    Then the Item will be saved

Sceanrio: Add item without descritpion 
    When I add an item without a descritpion 
    Then the Item will be saved

Sceanrio: Add item without name
    When I add an item without a name
    Then the item will not be saved
    And I will be informed the name is maditory

Sceanrio: Add item without number
    When I add an item without a number
    Then the item will not be saved
    And I will be informed the number is maditory

Sceanrio: Add item without date
    When I add an item without a date
    Then the item will not be saved
    And I will be informed the date is maditory

推荐阅读