如何在Robot框架中传递命令行参数以使其可用于所有库?

时间:2015-08-05 06:12:14

标签: python robotframework

我为我的功能测试开发了几个用于机器人框架的库,对于这些库,所有变量都来自variables.py文件。下面是variables.py的代码块:

#!/usr/bin/env python

import sys
import os
import optparse
import HostProperties
import xml.etree.ElementTree as ET
from robot.api import logger

testBed = 748
tree = ET.parse('/home/p6mishra/mybkp/testLibs/TestBedProperties.xml')

class raftGetTestBedProp(object):
  def GetTestBedNumber(self):
    _attributeDict = {}
    root = tree.getroot()
    for _tbProperties in root:
        for _tbNumber in _tbProperties:
            get_tb = _tbNumber.attrib
            if get_tb['name']== str(testBed):
                get_tb2 = _tbNumber.attrib
                return root, get_tb2['name']


  def GetTestBedProperties(self, root, testBedNumber):
    propertyList = []
    for _tbProperties in root:
        get_tb = _tbProperties.attrib
        for _tbProperty in _tbProperties:
            get_tb1 = _tbProperty.attrib
            if get_tb1['name']== str(testBedNumber):
              for _tbPropertyVal in _tbProperty:
                get_tb2 = _tbPropertyVal.attrib
                if 'name' in get_tb2.keys():
                    propertyList.append(get_tb2['name'])
    return propertyList


  def GetIPNodeType(self, root, testBedNumber):
    for tbNumber1 in root.findall('tbproperties'):
      for tbNumber in tbNumber1:
        ipv4support = tbNumber.find('ipv4support').text
        ipv6support = tbNumber.find('ipv6support').text
        lbSetup = tbNumber.find('lbSetup').text
        name = tbNumber.get('name')
        if name==str(testBedNumber):
            return ipv4support, ipv6support, lbSetup



obj1, obj2 = raftGetTestBedProp().GetTestBedNumber()
ipv4support, ipv6support, lbSetup = raftGetTestBedProp().GetIPNodeType(obj1, obj2)
AlltestBedProperties = raftGetTestBedProp().GetTestBedProperties(obj1, obj2)

HostPropertyDict = {}
for testBedProperty in AlltestBedProperties:
    try:
        val1 = getattr(HostProperties, testBedProperty)
        HostPropertyDict[testBedProperty] = val1
    except:
        logger.write("Error in the Configuration data. Please correct and then proceed with the testing", 'ERROR')


for indexVal in range(len(AlltestBedProperties)):
    temp  = AlltestBedProperties[indexVal]
    globals()[temp] = HostPropertyDict[temp]

此variables.py文件根据testbed编号返回HostProperties.py文件中定义的所有变量。 如果我在其他库中导入像from variables import *这样的库,它会为我提供所需的变量。

但问题是在这里我已经硬化了748所以它对我来说很好但我想从pybot command传递这个测试台编号信息并使其可用于我的Robot测试用例以及所有开发的库。< / p>

1 个答案:

答案 0 :(得分:2)

您可以发布用于调用这些Python文件的Robot Framework代码吗?我想你可以使用 pybot -v testBed:748 并将它作为参数传递给__init__你的类。如果没有看到如何启动Python变量,我不确定。

有点不同的方法是使用环境变量:

#!/usr/bin/env python

import sys
import os
import optparse
import HostProperties
import xml.etree.ElementTree as ET
from robot.api import logger

testBed = os.environ['testbed']
tree = ET.parse('/home/p6mishra/mybkp/testLibs/TestBedProperties.xml')

在启动pybot之前,只需定义此环境参数:

export testbed=748
pybot tests.txt