【问题标题】:Dealing with a large string in Gherkin with Scenario Outline使用场景大纲处理 Gherkin 中的大字符串
【发布时间】:2019-06-27 22:03:56
【问题描述】:

我将 Behat 用于 BDD 并使用 Scenario Outlines,因此我可以轻松地对其他数据进行相同的测试。但是我遇到了大文本的问题。请参见下面的示例:

Scenario Outline: create a thing
  When I click on "New"
  Then I should be at "/thing/new"
  When I fill in "title" with <title>
  When I fill in "description" with "description"
  When I click on "save"
  Then I should be at "/things"
  Then I should see <title> in the list
  When I click on <title>
  Then I should see <title>
  Then I should see <description>

  Examples:
    | title          | description |
    | "My new thing" | "a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string" |

您可以想象,如果有更多大文本或更多类型的值,这可能会很烦人。有解决方案吗?例如使用变量?可能是这样的:

$myvar = "a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string"

Scenario Outline: create a thing
  When I click on "New"
  Then I should be at "/thing/new"
  When I fill in "title" with <title>
  When I fill in "description" with "description"
  When I click on "save"
  Then I should be at "/things"
  Then I should see <title> in the list
  When I click on <title>
  Then I should see <title>
  Then I should see <description>

  Examples:
    | title          | description |
    | "My new thing" | $myvar      |

【问题讨论】:

    标签: bdd behat gherkin


    【解决方案1】:

    如果是我,我会写出更高级别的场景。你描述的需求是什么?如果是“事物”最多可以有 500 个字符(或其他)的描述,那么就这样说,而不是使用任意长字符串:

    When I fill in "description" with a 500 character description
    ....
    Then the new thing should have a description 500 characters long
     And the new thing's description should match the description entered
    

    然后,您的 When 步骤实现可以生成 500 个字符的 Lorem Ipsum 数据,将其输入表单并将其存储在 Scenario 上下文中以供稍后检查。

    它不漂亮但是:

    1. 它比随机的“长字符串”更好地描述了需求
    2. 它使功能文件更干净、更简洁

    可能值得应用相同的“我描述的是什么要求?”对场景的其余部分也有疑问。这里发生了很多事情,我个人会分成多个场景。

    【讨论】:

    • 感谢您的回答。尽管我确实认为使用真实世界的示例进行测试会更好。因此,在这种情况下,长字符串值将具有客户端想要赋予它的值。所以我想使用类似定义变量的东西来保持示例部分更干净。
    • 这一直是这些问题的问题所在,使它们足够通用以至于对每个人都有意义,但又不会太通用以至于没有所有细节。如果有一些真实世界的数据,那么我会将其放在一个单独的 Fixtures.php 文件中,然后它们将可供您的所有 Step 实现使用。我不太了解 Behat,无法准确推荐如何做到这一点,所以我会将详细答案留给其他人。
    • 我同意詹姆斯的观察。 Gherkin 应该描述系统的行为或用户的描述性动作。作为一个经验法则,我不会添加任何没有商业价值的东西。所以,点击按钮提交表单在大多数情况下没有商业价值,但提交表单本身就有商业价值。但是,回到你原来的问题,在你的情况下,在你的步骤定义中处理这个大文本可能是一个更好的选择。 Gherkin 不是像您描述的那样处理大字符串的好工具。
    【解决方案2】:

    我不认为我们可以像你用$myvar 举例说明的那样有变量。如果 James McCalden 的建议不适合您,将内容放在外部文件中可能与您的 $myvar 建议非常接近,例如:

    Scenario Outline: create a thing
      When I ... 
      ...
      Then I should see <title>
      Then I should see <description>
    
      Examples:
        | title          | description |
        | "My new thing" | /mydir/myvar.txt |
    

    那么 test resources 目录中的/mydir/myvar.txt 将包含:

    a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string a very large string
    

    最后在你的步骤课上:

     @Then("Then I should see (.+)")
     public void thenISouldSee(String param) {
           param = process(param);
           ...
     }
    
     private String process(String parameter) throws IOException, URISyntaxException {
           return parameter.charAt(0) == '/' ? readFile(parameter) : parameter;
     }
    
     private String readFile(String dir) throws IOException, URISyntaxException {
            File file = new File(BrandsContentManagementSteps.class.getResource(dir).toURI());
            return FileUtils.readFileToString(file, StandardCharsets.UTF_8);
     }
    

    请注意,FileUtils 需要导入 apache commons.io

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-04
      • 1970-01-01
      • 2017-12-01
      • 2023-02-10
      • 1970-01-01
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      相关资源
      最近更新 更多