代码之家  ›  专栏  ›  技术社区  ›  Cooper Yang

通过robot framework logger写入流时发生“OSError[Errno5]输入/输出错误”

  •  1
  • Cooper Yang  · 技术社区  · 7 年前

    我设计了一个机器人框架测试库,具体方法定义如下:

    import json
    from robot.api import logger
    

    ...

    @staticmethod
    def select_list_first_element_info(select_list, select_key):
        if select_list and isinstance(select_list, list) and select_key:
            data_dict = select_list[0]
            json_dict = json.loads(data_dict)
            if select_key in json_dict.keys():
                return json_dict.get(select_key)
            else:
                logger.error(r"%s cannot be found in keys of dict variable json_dict." % select_key, html=True)
                return ""
        else:
            logger.error(r"select_list is not valid list or select_key is null.", html=True)
            return ""
    

    但是,无论html是否为True,都会发生I/O错误。

    20180302 01:18:55.723 - INFO - +--- START KW: lhvapi.Select List First Element Info [ ${dc_list} | ${key_dcid} ]
    20180302 01:18:55.724 - ERROR - select_list is not valid list or select_key is null.
    20180302 01:18:55.727 - FAIL - OSError: [Errno 5] Input/output error
    20180302 01:18:55.727 - DEBUG - Traceback (most recent call last):
      File "/home/autorunner/virtual_env/lib/python3.6/site-packages/lhvapi/LhvRequests.py", line 162, in select_list_first_element_info
        logger.error(r"select_list is not valid list or select_key is null.", html=True)
      File "/home/autorunner/virtual_env/lib/python3.6/site-packages/robot/api/logger.py", line 131, in error
        write(msg, 'ERROR', html)
      File "/home/autorunner/virtual_env/lib/python3.6/site-packages/robot/api/logger.py", line 88, in write
        librarylogger.write(msg, level, html)
      File "/home/autorunner/virtual_env/lib/python3.6/site-packages/robot/output/librarylogger.py", line 44, in write
        LOGGER.log_message(Message(msg, level, html))
      File "/home/autorunner/virtual_env/lib/python3.6/site-packages/robot/output/logger.py", line 154, in _log_message
        self.message(msg)
      File "/home/autorunner/virtual_env/lib/python3.6/site-packages/robot/output/logger.py", line 141, in message
        logger.message(msg)
      File "/home/autorunner/virtual_env/lib/python3.6/site-packages/robot/output/console/quiet.py", line 28, in message
        self._stderr.error(msg.message, msg.level)
      File "/home/autorunner/virtual_env/lib/python3.6/site-packages/robot/output/console/highlighting.py", line 74, in error
        self.write(' ] %s\n' % message)
      File "/home/autorunner/virtual_env/lib/python3.6/site-packages/robot/output/console/highlighting.py", line 51, in write
        self.stream.write(console_encode(text, stream=self.stream))
    

    操作系统:CentOS Linux 7.4.1708版(核心)

    Python:v3.6.3

    Robot框架:v3.0.2

    2 回复  |  直到 7 年前
        1
  •  0
  •   Helio    7 年前

    我没有在Fedora Core 26上重现你的错误。 这是您修改的代码(删除了staticmethod注释):

    import json
    from robot.api import logger
    
    
    def select_list_first_element_info(select_list, select_key):
        """Example of select_list_first_element_info documentation.
        """
    
        if select_list and isinstance(select_list, list) and select_key:
            data_dict = select_list[0]
            json_dict = json.loads(data_dict)
            if select_key in json_dict.keys():
                return json_dict.get(select_key)
            else:
                logger.error(r"<b>%s</b> cannot be found in keys of dict variable <i>json_dict</i>." % select_key, html=True)
                return ""
        else:
            logger.error(r"<i>select_list</i> is not valid list or <i>select_key</i> is <b>null</b>.", html=True)
            return ""
    
    
    if __name__ == "__main__":
        myjson = ['{"customer_user_id": "toto","password_user_id": "tutu","vpws_name": "VPWS-POC-002"}']
    
        print("value: ")
        print(select_list_first_element_info(myjson, "customer_user_id"))
    

    这是一个示例测试文件:

    *** Settings ***
    Library           lhvapi.py
    
    *** Test Cases ***
    test keyword
        @{mydata}=    Create List    {"userid": "hisname","pass_userid": "secret","other_name": "Name"}
        ${myres}=    Select List First Element Info    ${mydata}    pass_userid
        Log To Console    ${myres}
        ${myres}=    Select List First Element Info    ${mydata}    username
        ${myres}=    Select List First Element Info    ${mydata}    ${None}
        ${myvar}=    Convert To String    ${mydata}
        ${myres}=    Select List First Element Info    ${myvar}    other_name
    

    然后在virtualenv中执行:

    (venv) [helio@localhost Robot]$ pip install robotframework
    Collecting robotframework
    Installing collected packages: robotframework
    Successfully installed robotframework-3.0.2
    (venv) [helio@localhost Robot]$ robot test_lhvapi.robot 
    ==============================================================================
    Test Lhvapi                                                                   
    ==============================================================================
    test keyword                                                          ..secret
    [ ERROR ] <b>username</b> cannot be found in keys of dict variable <i>json_dict</i>.
    [ ERROR ] <i>select_list</i> is not valid list or <i>select_key</i> is <b>null</b>.
    [ ERROR ] <i>select_list</i> is not valid list or <i>select_key</i> is <b>null</b>.
    test keyword                                                          | PASS |
    ------------------------------------------------------------------------------
    Test Lhvapi                                                           | PASS |
    1 critical test, 1 passed, 0 failed
    1 test total, 1 passed, 0 failed
    ==============================================================================
    Output:  /home/helio/Test/Robot/output.xml
    Log:     /home/helio/Test/Robot/log.html
    Report:  /home/helio/Test/Robot/report.html
    (venv) [helio@localhost Robot]$ python --version
    Python 3.6.4
    
        2
  •  0
  •   Cooper Yang    7 年前

    robot尝试记录时,重定向根课程stderr。 https://github.com/robotframework/robotframework/issues/2784