【问题标题】:How to write python function to test the matched strings (to use for Robot framework keyword)?如何编写python函数来测试匹配的字符串(用于机器人框架关键字)?
【发布时间】:2015-12-18 02:33:57
【问题描述】:

我正在为 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函数:

  1. 执行 windows 命令“ipconfig”
  2. 编写正则表达式以匹配 1.1.1.1
  3. 创建一个列表变量,mat1 = ['1.1.1.1']

现在我想设置条件,如果“match1”和“mat1”相等,我的测试应该通过。否则它应该在机器人框架中失败。

有没有人请教一下如何为此目的编写python函数?

请注意我不想在机器人框架中使用“应该匹配正则表达式”关键字。因为我知道无论我问什么,它都会做同样的事情。

【问题讨论】:

    标签: robotframework


    【解决方案1】:

    要使关键字通过,您无需执行任何操作,只需正常返回给调用者即可。要失败,您需要引发异常:

    def find_ip():
        ...
        if match1 != mat1:
            raise Exception('expected the matches to be similar; they are not")
    

    这在Returning Keyword Status 部分的机器人用户指南中有记录:

    报告关键字状态只需使用例外即可。如果 执行方法引发异常,关键字 status 为 FAIL,并且 如果正常返回,状态为PASS。

    日志、报告和控制台中显示的错误消息已创建 来自异常类型及其消息。有一般例外(对于 例如,AssertionError、Exception 和 RuntimeError),只有 使用了异常消息,和其他人一起创建的消息是 格式 ExceptionType: 实际消息。

    【讨论】:

      猜你喜欢
      • 2014-05-21
      • 2016-01-03
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 2016-04-03
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多