1

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 
0

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

yes, it is still prompting for the variable that should be set with the -i option at the command line
@pink_floyd668 I have made an edit, I think this is what you are looking for. Make sure to check out the linked questions and answer as well.
This worked to get me past the -i error. I'm now having further errors saying "execution stopped by user" which after researching it seems I am hitting a sys.exit() somewhere. I will continue to research thanks for the help.
@pink_floyd668 You are welcome. Yep probably a sys.exit if help needed make sure to post a new question about the new problem. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.