问题
I am trying to pass a list as a command line argument to a robot script using -v option. Since this is not directly supported i am trying some workaround like,
robot -v list_arg:a__b__c -E space:_ sample.robot
This partly does the job as i can see space separated values when i log them onto console. Likea b c
.
However i am unable to iterate over all the list members using FOR.I only see one item("a b c") being iterated.
Is there any other elegant way to pass Lists via command line in robot framework.?
回答1:
Although I don't like it much, I was able to iterate through the list created by Split String
from String
library.
*** Settings ***
Library String
*** Test Cases ***
List Of Variables From CLI
@{list_of_vars} Split String ${my_vars}
:FOR ${var} IN @{list_of_vars}
\ Log ${var} WARN
robot -v my_vars:1_2 -E space:_ -t "List Of Variables From CLI" .
回答2:
When passing data structures to Robot Framework using Variable Files would probably be a better option considering you're already converting one structure into a command line compatible one. Adding a variable file uses the -V c:/path/to/file.ext
syntax.
There are roughly two approaches:
Static: this is a text file containing the variable structures you seek. This can be in two formats:
- Python: declare the variable structures using regular Python syntax variable syntax
- YAML: using the YAML syntax it is possble to create Robot Framwork lists, dictionaries and strings.
Dynamic: in this scenario the Robot variables are generated and returned by Python code:
- Simple Python Function: using the
get_variables(arg)
function is the simplest way of returning a dynamic number of variables or a dynamic structure of data. - Dynamic Python Class: using a variable class it's possible to hide certain variables, have a fixed set of variables that are complemented by
init()
based on given input or have all of them generated dynamically.
- Simple Python Function: using the
In most cases the yaml structure is a good way of providing clean way of writing and maintaining an input file:
string: Hello, world!
integer: 42
list:
- one
- two
dict:
one: yksi
two: kaksi
original: &org
item1: foo
item2: bar
reference
org: *org
来源:https://stackoverflow.com/questions/44067832/how-to-pass-a-list-as-command-line-argument-in-robot-framework