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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 更新时间:2016/5/13 介绍 本文档所提供的编码规范,适用于主要的Python发行版中组成标准库的Pytho...
    超net阅读 5,913评论 0 15
  • 一、字符串在C#中,字符串是一系列不可修改的Unicode字符,创建字符串后,就不能修改它。要创建字符串,最常用的...
    CarlDonitz阅读 1,303评论 0 2
  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 3,272评论 0 4
  • 春天里 满含着笑意 轻轻地走来 风里尽是柔柔的暖 枯草地泛着绿意 光楞楞的枝绽着新芽 春天里 满含着憧憬 婷婷地走...
    月漫香江阅读 293评论 2 1
  • “反正生活始终都要过,为何不笑着活下去呢?” “笑笑没烦恼,你也笑一个吧。” 有时候遭遇了某些不好的事情,然后到了...
    暗阳玄日皇阅读 852评论 1 9