【问题标题】:How to dynamically iterate variables imported from yaml file in robot framework如何在机器人框架中动态迭代从 yaml 文件导入的变量
【发布时间】:2020-01-25 13:54:16
【问题描述】:

下面是我们用于变量的 device_details.yaml 文件,

Device1:
  IP: 192.168.23.21
  Port: 23
  admin_cred:
    username: admin
    password: Cisco123$
  nonadmin_cred:
    username: six 
    password: six

Device2:
  IP: 192.168.23.22
  Port: 23
  admin_cred:
    username: admin
    password: Cisco123$
  nonadmin_cred:
    username: six
    password: six

下面是机器人sn-p:

*** Settings ***
Variables    /tmp/robot/device_details.yaml

*** Test Case ***
Test
    Device Detail
***Keywords****
Device Detail
       Log   Device1 IP is ${Device1.IP}
       Log   Device1 port is ${Device1.Port}
       Log   Device1 admin username is ${Device1.admin_cred.username}
       Log   Device1 admin password is ${Device1.admin_cred.password}
       Log   Device1 non-admin username is ${Device1.nonadmin_cred.username}
       Log   Device1 non-admin password is ${Device1.nonadmin_cred.password}
       Log   Device2 IP is ${Device2.IP}
       Log   Device2 port is ${Device2.Port}
       Log   Device2 admin username is ${Device2.admin_cred.username}
       Log   Device2 admin password is ${Device2.admin_cred.password}
       Log   Device2 non-admin username is ${Device2.nonadmin_cred.username}
       Log   Device2 non-admin password is ${Device2.nonadmin_cred.password}

我们最终可能会在 yaml 文件中包含 200 到 300 个设备详细信息。 除了调用每个变量,有什么方法可以一次又一次地动态迭代变量吗?

【问题讨论】:

    标签: yaml robotframework


    【解决方案1】:

    最佳方法取决于您希望如何测试设备并记录结果。如果您总是在测试每个设备,那么在 Robot Framework 中循环,如下所示。但是,您也可以使用命令行参数加载variable file。然后,您将使用您的测试编排循环通过您的设备。

    devices.yaml

    Devices:
      a1:
        IP: 192.168.23.21
    
      b2:
        IP: 192.168.23.22
    

    test.robot

    *** Settings ***
    Library    Collections
    
    Variables    devices.yaml
    
    *** Test Case ***
    
        Test
            ${device_names}    Get Dictionary Keys    ${devices}
            FOR    ${device}    IN    @{device_names}
               Log   Device1 IP is ${devices}[${device}][IP]
            END
    

    【讨论】:

    • 感谢 Kootstra,这有帮助。是否可以根据每个设备 IP - ${devices}[${device}][IP] 动态创建测试用例。目前在单个测试用例中,使用您的解决方案,我们可以循环并传递IP地址来运行和执行它,但是所有设备都是通过循环在单个测试用例下测试的。问题是可能有 10 台设备或 300 台设备要测试。我的雇主希望为每个设备提供单独的测试用例。谢谢。
    • 当有人询问某事是否可行时,IT 中的答案总是“是”和/或“视情况而定”。上面的答案强调,您可以使用编排工具循环使用一组使用外部变量文件作为输入的选项。您可以将其与机器人框架标签结合使用,以过滤掉那些在特定场景中不需要的测试用例。
    猜你喜欢
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 2016-02-23
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 2018-01-23
    相关资源
    最近更新 更多