pink_floyd668
pink_floyd668

Reputation: 31

Trying to pass in a file as a command line option in Robot Framework

I am trying to test a python file using robot framework and part of my file takes in a -i option with the input file being provided from the command line. I want to be able to set the inputFile from the command line. If using the -i option in Robot Framework isn't an option, is there a way to explicitly set the inputFile variable in my .robot file?

Here is some code for reference:

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)

Here are some of the options I was trying but I'm not sure I understand how to pass in input arguments in 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.

Here is what my test file looks like:

*** 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

Upvotes: 1

Views: 1152

Answers (1)

Bence Kaulics
Bence Kaulics

Reputation: 7271

The arguments should be passed before test2.robot, so before the robot file or test folder. The correct order would be:

robot --variable inputFile:duplicate_module_simple_logging.py test2.robot

Then the ${inputFile} variable should be used in the tests. Its value will be duplicate_module_simple_logging.py.

Update to reflect edit on the question. To translate the problem it is actually what is described here: In Python, can I call the main() of an imported module?.

Your Python file with the argparser should be modified like described in this answer.

Here is an example:

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:])

and how to call from the test:

*** Settings ***
Library    var.py

*** Test Case ***
Test Calling Main
    main    -i    ${inputFile}

enter image description here

Upvotes: 2

Related Questions