如何在机器人框架中动态迭代从yaml文件导入的变量

问题描述 投票:0回答:1

下面是我们用于变量的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

下面是机器人代码段:

*** 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个设备详细信息。除了调用每个变量之外,还有什么方法可以动态地迭代另一个变量?

谢谢,

Mohan

yaml robotframework pyyaml
1个回答
0
投票
最佳方法取决于您要如何测试设备和注册结果。如果您始终在测试每个设备,请在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
© www.soinside.com 2019 - 2024. All rights reserved.