How can I specify arguments in the middle of the sentence in gherkin-style tests in robot framework?

扶醉桌前 提交于 2019-12-11 07:36:26

问题


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

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 )

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):
...

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}

回答1:


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

User keywords

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

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}

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.




回答2:


This keywords-section "rule" does the trick. Robot is very nit-picky about the format.

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

Note that the variable should be quoted.

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

The variable should be in quotes in the rule (first line), and preceded by 2 spaces in the value (second line).

This maps successfully to:

def there_is_an_alert_after_seconds(self, seconds):
    ...


来源:https://stackoverflow.com/questions/44844567/how-can-i-specify-arguments-in-the-middle-of-the-sentence-in-gherkin-style-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!