Python RobotFramework 中两个变量的总和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30975604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sum of two variables in RobotFramework
提问by buurkeey
I have two variables:
我有两个变量:
${calculatedTotalPrice} = 42,42
${productPrice1} = 43,15
I executed
我执行了
${calculatedTotalPrice} Evaluate ${calculatedTotalPrice}+${productPrice1}
I got
我有
42,85,15
How can I resolve it?
我该如何解决?
采纳答案by Laurent Bristiel
By default variables are string in Robot. So your first two statements are assigning strings like "xx,yy" to your vars. Then "evaluate" just execute your statement as Python would do. So, adding your two strings with commas will produce a list:
默认情况下,Robot 中的变量是字符串。因此,您的前两个语句将诸如“xx,yy”之类的字符串分配给您的变量。然后“评估”就像 Python 那样执行你的语句。因此,用逗号添加两个字符串将生成一个列表:
$ python
>>> 1,2+3,4
(1, 5, 4)
So you should use number variablesusing ${} and . (dots) for separator like in this example:
所以,你应该用数量变量使用$ {}和。(点)用于分隔符,如本例所示:
*** Test Cases ***
sum of variables
${calculatedTotalPrice} = set variable ${42.42}
${productPrice1} = set variable ${43.15}
${calculatedTotalPrice} = Evaluate ${calculatedTotalPrice}+${productPrice1}
log to console ${calculatedTotalPrice}
This will produce: $ pybot test.robot
这将产生:$ pybot test.robot
==============================================================================
Test
==============================================================================
sum of variables ...85.57
==============================================================================
回答by Mitch
Laurent's answer is almost always going to be the best course, but if for some reason you desire or require your Robot variables to be Strings that contain numbers, you can alternatively convert them to numbers inside of the Evaluate call:
Laurent 的答案几乎总是最好的课程,但如果出于某种原因您希望或要求您的 Robot 变量是包含数字的字符串,您也可以在 Evaluate 调用中将它们转换为数字:
*** Test Cases ***
Test1
${I1} = set variable 10
${I2} = set variable 5
${F1} = set variable 42.42
${F2} = set variable 57.15
${ISUM} = Evaluate int(${I1}) + int(${I2})
${FSUM} = Evaluate float(${F1}) + float(${F2})
log to console ${ISUM} ${FSUM}
This gives output:
这给出了输出:
Test1 ......15 99.57