【问题标题】:How to pass in a dict to a Robot Framework variable from the command-line如何从命令行将 dict 传递给 Robot Framework 变量
【发布时间】:2018-11-14 05:10:39
【问题描述】:

我正在尝试从我的 RF 文件中运行我的 python 测试中的 main(),其中包含可选的关键字参数范围、域和超时。如果我在 RF 文件中对 args 进行硬编码,测试工作正常,但我希望能够从命令行设置它们。我希望能够运行 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)

【问题讨论】:

    标签: python python-3.x dictionary command-line robotframework


    【解决方案1】:

    扩展您链接的示例代码可以这样完成:

    *** 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 变量的方式非常难看。如果变量文件是不可能的,我可能会改用jsoneval

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

    【讨论】:

    • 试过这个但得到错误“关键字'运行程序'预期 0 个非关键字参数,得到 1。”执行运行程序时。在我的测试用例中,在调用“运行程序 ${dict}”之前,我有“${dict} Convert JSON to Dictionary ${dict_args}”。你是如何让它发挥作用的?
    【解决方案2】:

    字典不能从命令行传递。导入 Python 结构的最佳方式是使用 YAML。这将允许您将这些作为变量直接加载到机器人中。

    可以通过三种方式加载 YAML 文件:

    1. command-line 使用--variablefile FileName.yaml
    2. Settings section 中作为Variable FileName.yaml
    3. 使用KeywordImport Variables FileName.yaml

    这三种方法各有利弊。如果您有需要导入的固定数据集,那么方法 2 就可以了。如果您知道在启动 Robot Framework 时应该使用哪个数据集,那么我会选择数字 1。如果只能在测试运行时做出该决定,请选择选项 3。

    【讨论】:

    • 完美!我发现这是做我想做的最好的方式。用更新的代码编辑了我的问题。
    猜你喜欢
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    • 1970-01-01
    • 2016-09-15
    • 2012-11-07
    • 1970-01-01
    相关资源
    最近更新 更多