【问题标题】:Trying to pass in a file as a command line option in Robot Framework尝试在 Robot Framework 中将文件作为命令行选项传递
【发布时间】:2021-04-25 02:40:50
【问题描述】:

我正在尝试使用机器人框架测试 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

【问题讨论】:

    标签: python linux unit-testing robotframework command-line-arguments


    【解决方案1】:

    参数应该在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}
    

    【讨论】:

    • 是的,它仍在提示应在命令行中使用 -i 选项设置的变量
    • @pink_floyd668 我已经进行了编辑,我认为这就是您要找的。请务必查看链接的问题和答案。
    • 这让我克服了 -i 错误。我现在有更多错误说“用户停止执行”,经过研究,我似乎在某个地方遇到了 sys.exit() 。感谢您的帮助,我将继续研究。
    • @pink_floyd668 不客气。是的,可能是sys.exit,如果需要帮助,请确保发布有关新问题的新问题。 :)
    猜你喜欢
    • 2018-11-14
    • 2015-11-20
    • 2015-12-13
    • 1970-01-01
    • 2015-09-12
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多