【问题标题】:How to call a Python function with parameters from RobotFramework?如何使用来自 RobotFramework 的参数调用 Python 函数?
【发布时间】:2020-05-21 15:26:35
【问题描述】:

我需要你的帮助来解决以下问题:

作为我的机器人框架测试的一部分,我还必须通过 Linux CLI 执行一些验证。我已经导入了 SSHLibrary,并且可以正确连接到我的 Linux 机器:

*** Keywords ***
Verify Service On All Nodes
    Open Connection  ${host}  prompt=REGEXP:[$|#]
    login   ${user}     ${user_pass}
    SSHLibrary.Read Until Prompt
    write   su - \n
    Read Until  Password:
    Write   ${root_pass}\n
    BuiltIn.sleep  10
    SSHLibrary.Set Client Configuration  prompt=~]
    Read Until Prompt
    Write   verify  #this stepp calls a shell script which does the required check
    BuiltIn.sleep  90
    SSHLibrary.Read Until Prompt
    ${output}  SSHLibrary.Read Until Prompt
    Log     ${output}

上面的 sn-p 将我的验证脚本的输出正确记录到报告中,但我想更进一步。

我编写了一个 Python 函数来验证上述脚本的输出。

def verify_service_state(ab11, ab12, s):
    import re
    servers = [ab11, ab12]
    for i in servers:
        r1 = re.search(r"^Checking listening ports on " + (i) + " for roles: \(AB\)" + "\nThere were no errors reported.", s, re.MULTILINE)
        if (r1):
            print("Listening ports are open " + (i))
        else:
            print("Not all listening ports are open on " + (i))
        r2 = re.search(r"^Checking process counts on " + (i) + " for roles: \(AB\)" + "\nThere were no errors reported.", s, re.MULTILINE)
        if (r2):
            print("There were no errors reported on " + (i))
        else:
            print("Not all services are working as expected on " + (i))

我已经根据这篇文章创建了一个 RF 库:Creating a robot framework library from existing python package 但是,我不清楚如何将属性传递给我的函数。

【问题讨论】:

    标签: python robotframework


    【解决方案1】:

    应该很简单:您可以使用Library \the\path\to\your\library\file.py 在 RF 中导入您的函数/库,或者在 PYTHONPATH 中添加您的文件(或文件夹)并导入包装函数的类。例如:Library ValidationLibrary

    然后,在您的测试中,您将您的函数用作任何其他关键字(其他示例请参见the documentation):

    Verify Service State      server1      server2     string
    

    或者,如果有可选参数:

    Verify Service State      ab11=server1      ab12=server2      s=string
    

    【讨论】:

    • 感谢 Nomce!非常感谢!
    • 不客气!如果它帮助您解决问题,您能否将答案标记为已接受?谢谢!
    • 我这样做了,但我的排名太低,所以没有出现在页面上。再次感谢!
    猜你喜欢
    • 2018-07-08
    • 2014-03-23
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    相关资源
    最近更新 更多