如何编写 python 函数来测试匹配的字符串(用于 Robot 框架关键字)?
How to write python function to test the matched strings (to use for Robot framework keyword)?
我正在为 python 中的机器人框架编写自定义库。由于某些原因,我不想使用内置库。
我的python代码:
import os
import re
output = "IP address is 1.1.1.1"
def find_ip():
cmd = 'ipconfig'
output = os.popen(cmd).read()
match1 = re.findall('.* (1.1.1.1).*',output)
mat1 = ['1.1.1.1']
if match1 == mat1:
print "PASS"
在上面的程序中我写了 python 函数到 :
- 执行windows命令"ipconfig"
- 编写正则表达式匹配1.1.1.1
- 创建一个列表变量,mat1 = ['1.1.1.1']
现在我想设置条件,如果 "match1" 和 "mat1" 相等,我的测试应该通过。否则它应该在 Robot 框架中失败。
任何人都可以提供有关如何为此目的编写 python 函数的想法吗?
请注意,我不想在 Robot Framework 中使用 "Should Match Regexp" 关键字。因为我知道它会按照我的要求做同样的事情。
要传递关键字,除了 return 正常向调用者执行任何操作外,您不需要执行任何操作。要失败,您需要引发异常:
def find_ip():
...
if match1 != mat1:
raise Exception('expected the matches to be similar; they are not")
机器人用户指南中的 Returning Keyword Status:
部分对此进行了记录
Reporting keyword status is done simply using exceptions. If an
executed method raises an exception, the keyword status is FAIL, and
if it returns normally, the status is PASS.
The error message shown in logs, reports and the console is created
from the exception type and its message. With generic exceptions (for
example, AssertionError, Exception, and RuntimeError), only the
exception message is used, and with others, the message is created in
the format ExceptionType: Actual message.
我正在为 python 中的机器人框架编写自定义库。由于某些原因,我不想使用内置库。
我的python代码:
import os
import re
output = "IP address is 1.1.1.1"
def find_ip():
cmd = 'ipconfig'
output = os.popen(cmd).read()
match1 = re.findall('.* (1.1.1.1).*',output)
mat1 = ['1.1.1.1']
if match1 == mat1:
print "PASS"
在上面的程序中我写了 python 函数到 :
- 执行windows命令"ipconfig"
- 编写正则表达式匹配1.1.1.1
- 创建一个列表变量,mat1 = ['1.1.1.1']
现在我想设置条件,如果 "match1" 和 "mat1" 相等,我的测试应该通过。否则它应该在 Robot 框架中失败。
任何人都可以提供有关如何为此目的编写 python 函数的想法吗?
请注意,我不想在 Robot Framework 中使用 "Should Match Regexp" 关键字。因为我知道它会按照我的要求做同样的事情。
要传递关键字,除了 return 正常向调用者执行任何操作外,您不需要执行任何操作。要失败,您需要引发异常:
def find_ip():
...
if match1 != mat1:
raise Exception('expected the matches to be similar; they are not")
机器人用户指南中的 Returning Keyword Status:
部分对此进行了记录Reporting keyword status is done simply using exceptions. If an executed method raises an exception, the keyword status is FAIL, and if it returns normally, the status is PASS.
The error message shown in logs, reports and the console is created from the exception type and its message. With generic exceptions (for example, AssertionError, Exception, and RuntimeError), only the exception message is used, and with others, the message is created in the format ExceptionType: Actual message.