How to write python function to test the matched strings (to use for Robot framework keyword)?

本小妞迷上赌 提交于 2020-01-01 19:22:27

问题


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

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"

In the above program I have written python function to :

  1. Execute a windows command "ipconfig"
  2. Written regular expression to match 1.1.1.1
  3. create a list variable, mat1 = ['1.1.1.1']

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

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

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.


回答1:


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")

This is documented in the robot user guide in the section 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.



来源:https://stackoverflow.com/questions/32694437/how-to-write-python-function-to-test-the-matched-strings-to-use-for-robot-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!