尝试在 Robot Framework 中将文件作为命令行选项传递
Trying to pass in a file as a command line option in 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
.
更新以反映对问题的编辑。 要翻译问题,它实际上是此处描述的内容:In Python, can I call the main() of an imported module?。
您的带有 argparser
的 Python 文件应该按照 answer 中的描述进行修改。
这是一个例子:
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}
我正在尝试使用机器人框架测试 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
.
更新以反映对问题的编辑。 要翻译问题,它实际上是此处描述的内容:In Python, can I call the main() of an imported module?。
您的带有 argparser
的 Python 文件应该按照 answer 中的描述进行修改。
这是一个例子:
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}