python - 尝试将文件作为 Robot Framework 中的命令行选项传递
问题描述
我正在尝试使用机器人框架测试 python 文件,我的部分文件采用 -i 选项,输入文件是从命令行提供的。我希望能够从命令行设置 inputFile。如果在 Robot Framework 中使用 -i 选项不是一个选项,有没有办法在我的 .robot 文件中显式设置 inputFile 变量?
这里有一些代码供参考:
parser = argparse.ArgumentParser(add_help=True, description='Usage')
parser.add_argument('-i', dest='input_file', required=True, help='Input module (servicenow, nagios, splunk etc.) containing its implementation')
parser.add_argument('-c', '--check', action='store_true', required=False, help='Flag to check connections to OVGD and input module.')
# Check and parse the input arguments into python's format
inputFile = parser.parse_args()
#inputFile = "duplicate_module_simple_logging.py"
inputModuleName = inputFile.input_file.split(".")[0]
#inputModuleName = inputFile.split(".")[0]
#gModuleName = inputModuleName
separator = os.sep
temp = inputModuleName.split(separator)
以下是我正在尝试的一些选项,但我不确定我是否了解如何在 Robot 中传递输入参数:
[root@xxxxxx]# robot --variable inputFile:duplicate_module_simple_logging.py test2.robot
==============================================================================
Test2
==============================================================================
Case1 | PASS |
------------------------------------------------------------------------------
Case2 | FAIL |
TypeError: string indices must be integers
------------------------------------------------------------------------------
Case3 | PASS |
------------------------------------------------------------------------------
Case4 | PASS |
------------------------------------------------------------------------------
Case5 usage: robot [-h] -i INPUT_FILE [-c]
robot: error: the following arguments are required: -i
[ ERROR ] Execution stopped by user.
这是我的测试文件的样子:
*** Settings ***
Library String
Library Collections
Library duplicate_main.py
Library duplicate_module_simple_logging.py
*** Variables ***
${inputFile} duplicate_module_simple_logging.py
*** Test Cases ***
Case1
${result1} = init logger_module
Should Be Equal As Integers ${result1} 0
Case2
${result2} = execute alert
Should Be Equal As Integers ${result2} 0
Case3
${result3} = cleanup
Should Be Equal As Integers ${result3} 0
Case4
${result4} = init 8
Should Be Equal As Integers ${result4} 0
Case5
${result5} = main
Should Be Equal As Integers ${result5} 0
解决方案
参数应该在 之前传递test2.robot
,所以在机器人文件或测试文件夹之前。正确的顺序是:
robot --variable inputFile:duplicate_module_simple_logging.py test2.robot
然后${inputFile}
应该在测试中使用该变量。其值为duplicate_module_simple_logging.py
。
更新以反映对问题的编辑。为了翻译这个问题,它实际上是这里描述的:在 Python 中,我可以调用导入模块的 main() 吗?.
您的 Python 文件argparser
应按照此答案中的说明进行修改。
这是一个例子:
import argparse
def main(*args):
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('-i', dest='input_file', required=True, help='Input module (servicenow, nagios, splunk etc.) containing its implementation')
inputFile = parser.parse_args(args)
print(inputFile)
if __name__ == '__main__':
main(sys.argv[1:])
以及如何从测试中调用:
*** Settings ***
Library var.py
*** Test Case ***
Test Calling Main
main -i ${inputFile}
推荐阅读
- oracle - 根据跨日期范围对订单进行分组
- firebase - Firebase Cloud Functions 部署错误,错误代码为“NoSuchKey”
- reactjs - 如何使用消息挂钩将值推送到数组中
- json - 如何使用 Observable 从文件中获取 json 数据
- api - 使用 api (blackberry qml) 关闭 Kodi
- flutter - 来自未知来源的 Pub 失败
- c# - 无法在 Unity 编辑器中加载预制件
- javascript - 一个标签的两个目标并在两个不同的div中显示两个结果
- azure-active-directory - Graph API - 使用 authorization_code 令牌创建应用程序
- javascript - 在离子角度应用程序中检索地址详细信息时的逗号问题