Behave 多行文本


包含在 """ 中的步骤之后的文本块将与该步骤链接。在这里,缩进被解析。开头的所有空格都从文本中删除,所有后续行必须至少有一个最小空格起跑线。

文本可通过上下文变量(在 step 函数中传递)中的 .text 属性由实现 Python 代码访问。

特征文件

以用户信息为标题的特征的特征文件如下:

Feature: User information
Scenario: Check login functionality
    Given user enters name and password
            """
            Newbiego Behave
          Topic – Multiline Text
            """
    Then user should be logged in

对应步骤实现文件

该功能对应的步骤实现文件如下:

from behave import *
@given('user enters name and password')
def step_impl(context):
#access multiline text with .text attribute
        print("Multiline Text: " + context.text)
@then('user should be logged in')
def step_impl(context):
        pass

运行特征文件后得到的输出如下所述,使用的命令是 Behave --no-capture -f plain .

Multiline Text

输出显示打印的多行文本。