【问题标题】:How can I specify arguments in the middle of the sentence in gherkin-style tests in robot framework?如何在机器人框架中的小黄瓜式测试中指定句子中间的参数?
【发布时间】:2017-12-04 07:15:00
【问题描述】:

使用 Robot Framework,我打算使用 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

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

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}

【问题讨论】:

    标签: robotframework gherkin


    【解决方案1】:

    机器人框架用户指南中有一节介绍了如何执行此操作。见Embedding arguments into keyword name

    用户关键词

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

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

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

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

    图书馆关键词

    您也可以使用机器人提供的keyword decorator 用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"
    

    其他功能

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 嵌入的参数不需要用引号括起来;这只是一个建议,因为 Bryan Oakley 在他的回答中解释了原因。在您引用的情况下,Robot 很挑剔,因为您有两个具有几乎相同签名的关键字;通过添加引号,第一个的签名发生了变化。事实上,如果第一个被命名为 there's an alert after ${time} seconds - 一个简单的语法变化,而第二个保持原样,RF 会很高兴地吞下并处理它们。
      猜你喜欢
      • 2016-10-14
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 2021-09-27
      • 2022-01-16
      • 2021-12-07
      • 2019-02-16
      • 1970-01-01
      相关资源
      最近更新 更多