我想将一些具有指定顺序的标签从 xml 文件加载到 python 中,如下所示:
<data>
<testset name="ts1" order="1" descrption="this is ts1 descrption">
<testcase name="tc1" order="1" />
<testcase name="tc2" order="2" />
</testset>
<testset name="ts2" order="2" descrption="this is ts2 descrption">
<testcase name="tc3" order="1" />
<testcase name="tc4" order="2" />
</testset>
</data>
我知道的方式很丑所以如果有更好的方法请告诉我:
class TS():
def __init__(self, name, order):
self.name = name
self.order = order
import xml.etree.ElementTree as ET
def parseXML():
ts_arr = []
tree = ET.parse('test_specs.xml')
root = tree.getroot()
for testset in root.iter('testset'):
i = 0
while i<len(ts_arr):
if testset.get('order')<ts_arr[i].order:
ts_arr.append(i, TS(testset.get('name'),testset.get('order')))
break
i += 1
...对于测试用例也是如此