在机器人框架的小黄瓜风格测试中,如何在句子中间指定自变量? [英] How can I specify arguments in the middle of the sentence in gherkin-style tests in robot framework?

查看:119
本文介绍了在机器人框架的小黄瓜风格测试中,如何在句子中间指定自变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using Robot Framework, I am intent on using Gherkin style tests since it is the lingua franca of BDD/ATDD. I can specify a test like this:

*** 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

,Robot会将其映射到我的测试库中的方法,而无需任何帮助:

and Robot will map this to methods in my test library without any help:

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之类的参数传递给测试库.

Cool. I can pass a parameter like level to the test library.

但是我不知道该怎么做,就是在子句的中间指定一个参数,如:

But what I don't know how to do, is specify a parameter which is in the middle of the clause, as in:

There there are 5 alerts sent

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

which I would like to map to this method in the test library:

def there_is_an_alert_after_seconds(self, seconds):
...

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

In this case the 5 is in the middle of the clause. In pytest.bdd there are decorators in the test library that you can use to specify how to parse the clause. What is the equivalent in robot?

我尝试了一下,但无济于事:

I tried this but to no avail:

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

推荐答案

机器人框架用户指南的一节介绍了如何执行此操作.请参见将参数嵌入关键字名称

The robot framework user guide has a section that covers how to do this. See Embedding arguments into keyword name

使用用户关键字非常简单:您无需使用[Arguments],只需将变量引用嵌入关键字名称中即可.这是用户指南中给出的示例:

It's very simple with user keywords: instead of using [Arguments] you merely embed the variable references in the keyword name. This is the example given in the user guide:

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

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

You use this keyword like you would expect:

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

图书馆关键字

您还可以使用关键字修饰符,使用python编写的关键字来完成此操作由机器人提供.例如:

Library Keywords

You can also do this with keywords written in python by using the keyword decorator provided by robot. For example:

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

使用引号

虽然不是绝对必要,但将引号引起来是一种很好的做法.用户指南中给出的示例是这样的:

Using quotes

While not strictly necessary, it's considered good practice to surround the argument with quotes. The example given in the user guide is this:

Select ${city} ${team}

如果使用Select Los Angeles Lakers调用它,机器人将如何知道${city}应该匹配"Los"还是"Los Angeles"?用引号引起来将解决该问题:

If you call this with Select Los Angeles Lakers, how will robot know if ${city} should match "Los" or "Los Angeles"? Placing quotes around it will solve that problem:

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

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

Of course, that means you also have to supply quotes when calling the keyword:

   Select "Los Angeles" "Lakers"

其他功能

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

Other features

It's also possible to use regular expressions to match arguments. For a comprehensive description of how to do that, see the user guide.

这篇关于在机器人框架的小黄瓜风格测试中,如何在句子中间指定自变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆