在机器人框架内定义包含变量的关键字

时间:2018-11-14 09:38:08

标签: python templates robotframework bdd gherkin

我目前正在将机器人框架用于具有Gherkin语言策略(“何时提供”)的项目。

我的功能文件如下:

*** Settings ***
Documentation
... In Order to eat a dessert safely, 
... As a king
... I want to not take a lethal dessert 

Library eat_or_pass.Eat_or_pass


*** Test cases ***
Lethal Dessert
    [Template]  The result of ${hungriness} should be ${dessert}
    very hungry apple pie   
    hungry      biscuit
    not very hungry apple


*** Keywords ***
The result of ${hungriness} should be ${dessert}
    Given the king is ${hungriness}
    Then the related dessert is ${dessert}

我想将关键字“鉴于国王是$ {hungriness}”链接到python模块Eat_or_pass.py中包含的python定义,该模块当前实现如下:

class Eat_or_pass(object):

def given_the_king_is_hungriness(self):
    pass

运行机器人框架时,出现此错误: “致命甜点|失败| 找不到名称为“国王为$ {hungriness}”的关键字。” 而且我不知道如何解决。有人可以帮助我解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

机器人代码:

*** Settings ***
Documentation
...    In Order to eat a dessert safely,
...    As a king
...    I want to not take a lethal dessert
Library    ./EatOrPass.py

*** Test Cases ***
Lethal Dessert
    [Template]    The Result Of ${hungriness} Should Be ${dessert}
    very hungry    apple pie
    hungry    biscuit
    not very hungry    apple

*** Keywords ***
The Result Of ${hungriness} Should Be ${dessert}
    Given The King Is ${hungriness}
    Then The Related Dessert Is ${dessert}

python库:

from robot.api.deco import keyword


class EatOrPass(object):

    @keyword('The King Is ${hungriness}')
    def the_king_is(self, hungriness):
        pass

    @keyword('The Related Dessert Is ${dessert}')
    def the_related_dessert_is(self, dessert):
        pass

我建议您将CamelCase用于python,并为RF使用4个空格(更好的可读性)。

答案 1 :(得分:-1)

*** Keywords ***
The result of ${hungriness} should be ${dessert}
    Given The king Is Hungriness   

应该给予国王饥饿感 ,而不给予国王$ {hungriness}

class Eat_or_pass(object):
  def given_the_king_is_hungriness(self):
  pass