如何编写python函数来测试匹配的字符串(用于Robot框架关键字)? [英] How to write python function to test the matched strings (to use for Robot framework keyword)?

查看:41
本文介绍了如何编写python函数来测试匹配的字符串(用于Robot框架关键字)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 python 为机器人框架编写一个自定义库.由于某些原因,我不想使用内置库.

I am writing a custom library for robot framework in python. I don't want to use builtin library for some reasons.

我的python代码:

My python code :

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函数:

In the above program I have written python function to :

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

现在我想设置条件,如果match1"和mat1"相等,我的测试应该通过.否则它应该在 Robot 框架中失败.

Now I want to put condition like, if "match1" and "mat1" are equal my TEST should PASS. else it should fail in Robot framework.

任何人请给出如何为此目的编写python函数的想法?

Any one please give idea on how to write python function for this purpose?

请注意,我不想在 Robot Framework 中使用Should Match Regexp"关键字.因为我知道无论我要求什么,它都会做同样的事情.

Please note I dont want to use "Should Match Regexp" keyword in Robot Framework. Because I know it will do the same whatever I am asking.

推荐答案

为了让关键字通过,你不需要做任何事情,除了正常返回给调用者.要失败,您需要引发异常:

To make a keyword pass, you don't need to do anything except return normally to the caller. To fail, you need to raise an exception:

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

机器人用户指南的返回关键字状态部分对此进行了记录:

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

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.

创建日志、报告和控制台中显示的错误消息从异常类型及其消息.有一般例外(对于例如,断言错误、异常和运行时错误),只有使用异常消息,与其他消息一起,在ExceptionType: 实际消息格式.

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函数来测试匹配的字符串(用于Robot框架关键字)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆