问题描述
我正在探索使用 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
我该如何在行为中做类似的事情?我在考虑也许在内部使用given
,when
和then
装饰器 step_impl
函数...
解决方法
使每个先前的消息成为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,这很适合BDD测试的Give-When-Then风格。您也不会用一堆与该场景没有直接关系的Then来弄乱您的场景。