首页 > 解决方案 > 如何使用来自 RobotFramework 的参数调用 Python 函数?

问题描述

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

作为我的机器人框架测试的一部分,我也必须通过 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}

上面的代码片段将我的验证脚本的输出正确记录到报告中,但我想更进一步。

我编写了一个 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 库:从现有的 python 包创建一个机器人框架库但是,我不清楚如何将属性传递给我的函数。

标签: pythonrobotframework

解决方案


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

然后,在您的测试中,您将函数用作任何其他关键字(请参阅文档以获取其他示例):

Verify Service State      server1      server2     string

或者,如果有可选参数:

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

推荐阅读