首页 > 解决方案 > 尝试将文件作为 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

标签: pythonlinuxunit-testingrobotframeworkcommand-line-arguments

解决方案


参数应该在 之前传递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}

在此处输入图像描述


推荐阅读