【问题标题】:Define a keyword containing a variable within robot framework在机器人框架中定义包含变量的关键字
【发布时间】:2018-11-14 20:18:48
【问题描述】:

我目前正在将机器人框架用于具有 Gherkin 语言策略(Given When Then)的项目。

我的功能文件如下:

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

我想将关键字“Given the king is ${hungriness}”链接到包含在 Python 模块 Eat_or_pass.py 中的 Python 定义,目前实现如下:

class Eat_or_pass(object):

def given_the_king_is_hungriness(self):
    pass

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

【问题讨论】:

    标签: python templates robotframework bdd gherkin


    【解决方案1】:

    机器人代码:

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

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

    【讨论】:

    • 它可能正在工作,但我的笔记本电脑上没有安装robot.api.deco,我也不允许安装它。您还有其他解决方案吗?
    • 你总是可以在没有嵌入参数的情况下做到这一点。在这种情况下,不需要装饰器,您只需定义关键字并将其与普通参数一起使用。你可以吗?你能自己管理吗?
    • 当然可以,但这意味着我必须增加测试用例的数量!反正我能搞定,非常感谢你的帮助
    • 我不这么认为。您的 python 定义的关键字将没有嵌入的参数,但一个机器人定义的关键字可以保持原样。所有建议的机器人代码都可以像答案一样,除非您将基于 python 的关键字称为Given The King Is<four spaces here>${hungriness}
    • 我使用了robot.api.deco公共库的代码源,我就是这样实现的,但是还是没有找到关键字。您还有其他解决方案吗?
    【解决方案2】:
    *** Keywords ***
    The result of ${hungriness} should be ${dessert}
        Given The king Is Hungriness   
    

    应该是Given the king Is Hungriness而不是Given the king is ${hungriness}

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

    【讨论】:

    • 如果我这样做,机器人不会考虑到我有各种各样的饥饿变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 2020-08-30
    • 2013-11-08
    • 2020-04-21
    • 2020-11-06
    • 2016-05-07
    相关资源
    最近更新 更多