Lettuce:多行字符串

已准备弃用Lettuce,入Behave的坑了。但还是会继续把Lettuce官网简陋翻译更新完成~

现在想象一下,你正在编写一个应用程序来处理字符串。编写测试时,你可能会发现自己希望在步骤中放置多行字符串。
多行字符串将发挥作用。

Feature: Split a string into multiple lines on spaces
  In order to make strings more readable
  As a user
  I want to have words split into their own lines

  Scenario: Split small-ish string
    Given I have the string "one two three four five"
    When I ask to have the string split into lines
    Then I should see the following:
      """
      one
      two
      three
      four
      five
      """

一个只有三个引号(“””)的行用来表示多行字符串的开头和结尾。
现在,让我们定义一个步骤展示如何使用它。

from lettuce import step

@step('I should see the following:')
def i_should_see_the_following(step):
    assert step.multiline == """one
two
three
four
five"""

非常简洁明了。
注意空格被去掉了,在开头或结尾没有一个换行符。这是由于解析器去掉了空行,前导和末尾空格。
如果你需要空行、前导或末尾空格,你可以在行的开始和/或结束使用双引号,他们将用引号保存空格,级联其他单行线。
例如

Feature: Split a string into multiple lines on spaces
  In order to make strings more readable
  As a user
  I want to have words split into their own lines

  Scenario: Split small-ish string
    Given I have the string "one two three four five"
    When I ask to have the string split into lines
    Then I should see the following:
      """
     " one
     " two  "
     "  three   "
     "   four    "
     "    five     "
     "
      """

我们可以这样检验:

from lettuce import step

@step('I should see the following:')
def i_should_see_the_following(step):
    assert step.multiline == '\n'.join([
    ' one',
    '  two  ',
    '   three   ',
    '    four    ',
    '     five     ',
    ''])

诚然,这是一个勾,但没有新的方式在当前解析器实现一个feature定义只有一部分保留空白。
注意第一行的结尾没有空格,因此在它的结尾不需要一个引号。
还要注意的是,如果在你的字符串一行的开始,需要双引号,则必须用两个双引号开始行,这样第一个引号将被去掉。


上一篇:Lettuce Datatable
下一篇:Lettuce: Scenario Outlines

你可能感兴趣的:(Lettuce:多行字符串)