【问题标题】:Nesting Scenarios? How could I have a Scenario use other Scenerios in its Given statement?嵌套场景?我如何让一个场景在其给定语句中使用其他场景?
【发布时间】:2020-08-26 22:25:48
【问题描述】:

我正在探索使用 behave 来测试聊天机器人。

我可以定义简单的场景,比如

Scenario: Asked a widget function question
   Given a new dialog
   When the visitor asks
   """
   What do your widgets do?
   """
   Then bot should explain widget function
Scenario: Asked a widget cost question
   Given a new dialog
   When the visitor asks
   """ 
   How much does your widget cost?
   """
   Then bot should provide widget cost

问题在于一个好的聊天机器人需要保留上下文。如果它成功地保留了上下文,那么顺序很重要。如果上述两种情况都通过了,这并不意味着一个接一个地询问这些问题的对话不会引发错误。

为此,您需要这样的场景

Scenario: Asked a widget function question and price question
   Given a new dialog
   When the visitor asks
   """
   What do your widgets do?
   """
   And the bot explains widget function

   When the visitor asks
   """ 
   How much does your widget cost?
   """
   Then bot should provide widget cost

当然,为每个想要测试的对话编写一个在 Given 块中包含对话前导码的场景会非常重复。

我宁愿以某种方式“嵌套”场景。像这样的东西(我知道它是无效的)。

 Scenario: Asked a widget cost question
   Given user Asked a widget function question
   When the visitor asks
   """ 
   How much does your widget cost?
   """
   Then bot should provide widget cost

我怎么能在 behave 中做这样的事情?我在考虑也许使用givenwhenthen 装饰器inside step_impl 函数...

【问题讨论】:

    标签: bdd python-behave


    【解决方案1】:

    将之前的每条消息都设为Given

    Scenario: Asked a widget function question and price question
        Given a new dialog
        And the visitor already asked
            """
            What do your widgets do?
            """
        When the visitor asks
            """
            How much does your widget cost?
            """
        Then bot should provide widget cost
    

    前面问题的Given 语句可以显式等待正确答案,也可以在继续下一步之前等待任何答案。这样,获取小部件价格的先决条件清楚地表示为 Given's,非常适合 BDD 测试的 Given-When-Then 样式。您也不会用一堆与场景没有直接关系的 Then 来混淆您的场景。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多