如何在机器人框架中的小黄瓜式测试中指定句子中间的参数?



使用机器人框架,我打算使用Gherkin风格的测试,因为它是BDD/ATDD的通用语言。我可以指定这样的测试:

*** Test Cases ***
New alert
    Given there were no alerts so far
    When there is an alert
    Then an alert should be sent with level  LOW

机器人会在没有任何帮助的情况下将其映射到我的测试库中的方法:

def there_were_no_alerts_so_far(self):
    assert(self.alert == None)
def there_is_an_alert(self):
    self.alert = self.alert_processor.alert()
def an_alert_should_be_sent_with_level(self, level):
    assert_not_none(self.alert )
    assert_equal(self.alert.level.name, level )

凉。我可以将level这样的参数传递给测试库。

但是我不知道该怎么做的是指定一个位于子句中间的参数,如下所示:

There there are 5 alerts sent

我想在测试库中映射到此方法:

def there_is_an_alert_after_seconds(self, seconds):
...

在这种情况下,5位于子句的中间。 在 pytest.bdd 中,测试库中有一些装饰器,可用于指定如何解析子句。 机器人中的等价物是什么?

我试过这个,但无济于事:

*** Keywords ***
there is an alert after ${time} seconds
    there is an alert after seconds ${time}

机器人框架用户指南中有一节介绍了如何执行此操作。请参阅在关键字名称中嵌入参数

用户关键字

用户关键字非常简单:您只需在关键字名称中嵌入变量引用,而不是使用[Arguments]。这是用户指南中给出的示例:

 *** Keywords ***
 Select ${animal} from list
    Open Page    Pet Selection
    Select Item From List    animal_list    ${animal}

您可以像预期的那样使用此关键字:

*** Test Cases ***
Example
    Select Dog from list

库关键字

您也可以使用机器人提供的关键字装饰器,使用用python编写的关键字来执行此操作。 例如:

from robot.api.deco import keyword
@keyword('Select ${animal} from list')
def select_animal_from_list(animal):
    ...

使用引号

虽然不是绝对必要的,但用引号括起来被认为是很好的做法。用户指南中给出的示例如下:

Select ${city} ${team}

如果你用Select Los Angeles Lakers来称呼它,机器人怎么知道${city}应该匹配"Los"还是"Los Angeles"?在它周围加上引号将解决这个问题:

Select "${city}" "${team}"

当然,这意味着您在调用关键字时还必须提供引号:

   Select "Los Angeles" "Lakers"

其他功能

也可以使用正则表达式来匹配参数。有关如何执行此操作的全面说明,请参阅用户指南。

这个关键字部分"规则"可以解决问题。 机器人对格式非常挑剔。

*** Test Cases ***
Second alert soon after, first escalation
    Given there is an alert
    When there is an alert after "7" seconds

请注意,变量应用引号括起来。

*** Keywords ***
there is an alert after "${time}" seconds
    there is an alert after seconds  ${time}

变量应在规则(第一行(中用引号引起来,并在值(第二行(中前面有 2 个空格。

这将成功映射到:

def there_is_an_alert_after_seconds(self, seconds):
    ...

相关内容

最新更新