【六】behave对大文本节使用多行文本Multi-line Text实例五

目标:对大文本使用多行文本(三重引用文本),在场景中添加注释,并打印出来。

三引号字符串:提供了一个可以使用大文本作为步骤参数的方法,一般来说,这么多文字不适合一行。

1.编写Feature Test功能测试

# file:features/tutorial05_step_data.feature
# Created by bxf at 2019/11/8

Feature: 你我
  Scenario: 忆往事
    Given 错过了吧
    """
    当初还是不够热爱吧不过倒是显得你的选择那么有道理
    """
    When 想回到最初的我们
    Then 才发现没有时光机

以上就是在场景中添加注释,可以通过"""来输出更多的注释信息,这些注释信息称为context的".text"属性值,如上图的Given,同理也可以在When和Then下添加注释。

2.提供Miss域模型

# file:features/steps/Miss.py
#encoding:utf-8
#date:2019/12/6 18:24
#@Author:sunny

class Miss(object):
    def __init__(self,text=None):
        self.text=None
        self.memory=False

    def Memory(self):
        self.memory=True

    def seems_like_ToBe_Togther_Again_TimeMachine(self):
        """
        Thought we could be together
        :return:
        """
        assert self.text is not None
        assert self.memory
        if self.text.startswith("当初"):
            return "时光机"
        else:
            return "UNKNOWN"

3.提供自动化测试

# file:features/steps/step_tutorial05.py
#encoding:utf-8
#date:2019/11/8 14:30
#@Author:sunny

from behave import *
from hamcrest import *
from features.steps.Miss import Miss

@given('错过了吧')
def step_impl(context):
    miss=getattr(context,"miss",None)
    if not miss:
        context.miss=Miss()
  #使用context.text来获取这段text,context.text也可以在每一个step function中传递使用。 context.miss.text
=context.text

@when(
'想回到最初的我们') def step_impl(context): context.miss.Memory() @then('才发现没有{Time_machine}') def step_impl(context,Time_machine): assert_that(Time_machine,equal_to(context.miss.seems_like_ToBe_Togther_Again_TimeMachine()))

运行结果:

E:\exercise\producer2-test>behave
Feature: 你我 # features/tutorial05_step_data.feature:3

  Scenario: 忆往事   # features/tutorial05_step_data.feature:4
    Given 错过了吧    # features/steps/step_tutorial05.py:9
      """
      当初还是不够热爱吧不过倒是显得你的选择那么有道理
      """
    When 想回到最初的我们 # features/steps/step_tutorial05.py:16
    Then 才发现没有时光机 # features/steps/step_tutorial05.py:20

1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.001s

E:\exercise\producer2-test>
View Code

以上结果运行可以看出已经将Given的注释打印出来了。

posted @ 2019-12-09 10:43  做一只热爱生活的小透明  阅读(184)  评论(0)    收藏  举报