简体   繁体   English

在Robot Framework中测试python函数

[英]Test a python function in Robot Framework

I am a new user and I didn't find a solution for a doubt about the execution of my script, wrote in python, in Robot Framework. 我是新用户,我没有找到解决方案来怀疑我在Robot Framework中用python编写的脚本的执行情况。

The script works when I execute it on python compiler but when I execute the test case on Robot Framework, this error is showed: 当我在python编译器上执行脚本时该脚本起作用,但是当我在Robot Framework上执行测试用例时,将显示此错误:

===========================================================================
TestProvaPower
===========================================================================
TestPowerAngelo                                                    | FAIL |
No keyword with name 'power' found.
---------------------------------------------------------------------------
TestProvaPower                                                     | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
===========================================================================
Output:  c:\users\user\appdata\local\temp\RIDEjtznzw.d\output.xml

I think that this error is shown because it is necessary to pass the arguments and parameters. 我认为显示此错误是因为有必要传递参数和参数。

Please, how can I pass these values in Robot Framework? 请,如何在Robot Framework中传递这些值?

The test suite is: 测试套件是:

** Settings **
Library           ../../../../../Users/User/workspace/TestAngelo18.09/TestProva22.py

** Test Cases **
TestPowerAngelo
    power    base    exponent
    push    ${base}    ${exponent} 

While my Python script is: 虽然我的Python脚本是:

base = input("Insert base")
exponent =input("Insert exponent")

def power(base,exponent):
    result=base**exponent
    print "%d to the power of %d is %d" %(base,exponent,result)

power (base,exponent)

As part of your module definition, you are getting input from the user. 作为模块定义的一部分,您正在从用户那里获得输入。 When a module is being imported, you cannot use the standard input stream so an EOFError occurs. 导入模块时,无法使用标准输入流,因此会发生EOFError。 Below is a modified version of your library that is still testable via direct execution. 以下是您库的修改后的版本,仍可以通过直接执行对其进行测试。

def power(base, exponent):
    result = base**exponent
    return result

if __name__ == '__main__':
    base = input("Insert base")
    exponent = input("Insert exponent")
    result = power(base,exponent)
    print "%d to the power of %d is %d" %(base, exponent, result)

Instead of using complex path in the Library import try setting python path with pybot eg 尝试在pybot中设置python路径,而不是在库导入中使用复杂路径

pybot --pythonpath /path/to/libs/where/py/file/is

And in the test suite file import it using just the name eg without the .py suffix. 并在测试套件文件中仅使用名称将其导入,例如,不带.py后缀。

Library    TestProva22

RF treats arguments as strings by default. 默认情况下,RF将参数视为字符串。 For literals, you can surround them with ${} or variables use Convert To Integer first. 对于文字,可以将它们用${}括起来,或者变量首先使用Convert To Integer Something like this should work: 这样的事情应该起作用:

${result} =  power   ${2}  ${4}
Should Be Equal As Integers  ${result}  16

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM