我尝试使用可选的关键字参数range、domain和timeout从我的RF文件中运行python测试中的main()。如果我在RF文件中对参数进行硬编码,测试就能正常工作,但我希望能够从命令行设置它们。我希望能够运行的RF文件给任何,所有,或没有上面列出的3个参数与类似于robot -v dict_args:range=5_domain=4 -E space=_ run_test.robot
的东西。根据文档,“只有标量变量可以使用这种语法设置,并且它们只能获取字符串值”,但是我在这里见过能够为列表(How to pass a list as command line argument in robot framework.?)编写变通方法的人。我还没能在字典上找到任何东西,任何建议/帮助都将不胜感激。
run_test.robot:
*** Settings ***
Library my_test
Library Process
*** Test Cases ***
Run RNTester
Run Program ${dict-args}
*** Keywords ***
Run Program
[Arguments] &{configuration}
main &{configuration}
my_test.py:
def main(**kwargs):
#process args
if 'range' in kwargs:
range_ = kwargs['range'].split(':')
range_ = list(map(int,range_))
if len(range_) == 1:
range_.insert(0,0)
test_count = range_[1] - range_[0]
else:
range_ = None
test_count = 0
if 'domain' in kwargs:
domain = kwargs['domain'].split(',')
else:
domain = None
if 'timeout' in kwargs:
timeout = int(kwargs['timeout'])
else:
timeout = 30
run_test(domain, range_, test_count, timeout)
修复了按照@jil的建议编辑我的代码以写入yaml文件。添加了一个YAMLCreator.py来制作我的RF文件导入的yaml文件。这是我现在所拥有的
run_test.robot:
*** Settings ***
Library my_test
Variables ${testname}.yaml
*** Test Cases ***
Run RNTester
Run Program &{test_name}
*** Keywords ***
Run Program
[Arguments] &{configuration}
main &{configuration}
YAMLCreator.py:
import yaml
import sys
def make_dict(**kwargs):
data = dict()
for key in kwargs:
value = kwargs[key]
data[key] = value
return data
if __name__ == '__main__':
args = sys.argv
test_name = args[1]
data = make_dict(**dict(arg.split('=') for arg in args[2:]))
test_name_dict=dict()
test_name_dict[test_name] = data
with open(test_name + '.yaml', 'w') as outfile:
yaml.dump(test_name_dict, outfile, default_flow_style=False)
发布于 2018-06-05 15:42:48
不能从命令行传递字典。导入Python结构的最好方法是使用YAML。这将允许您将这些作为变量直接加载到Robot中。
可以通过以下三种方式加载YAML文件:
使用--variablefile FileName.yaml
Import Variables FileName.yaml
a
这三种方法各有利弊。如果你有一个固定的数据集,你需要导入,那么方法2就可以了。如果你知道在Robot Framework开始时应该使用哪个数据集,那么我会选择第一个。如果这个决定只能在你的测试运行时做出,那么选择选项3。
发布于 2018-06-05 05:08:11
扩展您链接的示例代码可以像这样完成:
*** Settings ***
Library String
*** Keywords ***
Convert String To Dictionary
[Arguments] ${my_dict_string}
@{list_of_items} Split String ${my_dict_string}
${my_dict} = Create Dictionary @{list_of_items}
[Return] ${my_dict}
但就我个人而言,我觉得这种传递dict变量的方式相当难看。如果变量文件是不可能的,我可能会使用json
和eval
:
*** Keywords ***
Convert JSON to Dictionary
[Arguments] ${json_string}
${d}= evaluate json.loads('''${json_string}''') json
[Return] ${d}
然后这样叫它:
robot -v 'dict_args:{"range":5, "domain":4}' run_test.robot
https://stackoverflow.com/questions/50687810
复制相似问题