基于关键字状态从侦听器测试机器人框架中的状态更新

时间:2021-03-17 17:06:25

标签: python python-3.x robotframework

我设计了测试套件,我在其中调用侦听器来运行基于关键字失败日志和状态的关键字。

机器人代码

*** Settings ***
Documentation    Suite description
Library     RunOnFailureLibrary.py
Test Template   Run Keyword And Ignore Error
Suite Setup    Set Keyword To Run On Failure    Test Keyword    key     first  

*** Test Cases ***
Test 1
    log     test
    fail  something went wrong
    log     test1

Test 2
    pass execution  test

Test 3

    log     test
    fail    something 
    log     test1


*** Keywords ***
Test Keyword
    [Arguments]     ${arg1}     ${arg2}     ${arg3}
    log     ${arg1}
    log     ${arg2}
    log     ${arg3}

监听器代码:RunOnFailureLibrary.py

from robot.api import logger
from robot.libraries.BuiltIn import BuiltIn


class RunOnFailureLibrary(object):
    ROBOT_LISTENER_API_VERSION = 2
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'
    ROBOT_LIBRARY_VERSION = 0.1

    def __init__(self):
        self.ROBOT_LIBRARY_LISTENER = self
        self.keyword_to_run_on_faiure = None
        self.keyword_args = None
        self.log_Error= None

    def set_keyword_to_run_on_failure(self, keyword, *args):
        # print("args",args)
        if len(args) > 0:
            self.keyword_args = args
        self.keyword_to_run_on_faiure = keyword

    def _log_message(self, message):
        self.log_Error=message["message"]


    def _end_keyword(self, name, attributes):
        if attributes['status'] == 'FAIL' and self.log_Error=="something went wrong":
            logger.info(f"Running keyword:'{self.keyword_to_run_on_faiure}' on failure!")
            if attributes['args'] is None:
                    logger.info("This doesnt Looks like healing")
                    return False
            else:
                print(*self.keyword_args)
                BuiltIn().run_keyword(self.keyword_to_run_on_faiure,self.keyword_args[0],attributes["args"][0],
                                      self.keyword_args[1])

globals()[__name__] = RunOnFailureLibrary

正如您从机器人代码中看到的,如果关键字的 Test Keywordstatus 并且日志错误为 fail,我将根据关键字的 something went wrong 调用 Run Keyword And Ignore Error。如果不满足此条件,我们将不会运行此关键字。

问题在于我在测试模板 Test Keyword 中使用的以确保所有关键字都在测试用例中运行,因此无论状态如何,它都会被标记为通过,这是可以理解的。

但是我想实现类似的东西,如果调用了侦听器关键字 Test Keyword,我想根据此关键字的成功或失败来更新测试用例的状态。因此测试用例 1 结果状态必须基于 Test Template

在测试用例 3 中,由于错误消息与条件不匹配,侦听器不会调用关键字,因此测试用例必须失败。但由于let mut put_update_remove = vec![ KeyValue::Put { key: 1, value: 1 }, KeyValue::Update { key: 2, value: 2 }, KeyValue::Remove { key: 3 }, ]; let mut remove_expire = vec![ KeyValue::Remove { key: 1 }, KeyValue::Expire { key: 2, time: SystemTime::now(), }, ]; put_update_remove.append(&mut remove_expire);

,它会显示为通过

有什么办法,我们可以处理这个

0 个答案:

没有答案