我没有在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