一、背景
使用SoapUI编辑完接口测试文件保存后是一个xml文件,在做接口自动化平台二次开发的时候需要解析xml获取到API、TestSuite、TestCase、TestStep、Assertion等信息,本文使用的是ElementTree的方式解析的,记录下研究成果
二、接口信息
SoapUI中展示的Interface Summary 信息 和 Test Summary信息
三、XML解析代码
1 import xml.etree.ElementTree as ET 2 3 4 def xmlParse(): 5 ‘‘‘解析APIName,APIPath,suiteName,caseName,stepName‘‘‘ 6 7 apiCount, caseCount, stepCount, assertCount, suiteCount = 0 8 9 file_path = "D:\\api\\xml\\1002-soapui-project.xml" 10 tree = ET.parse(file_path) 11 root = tree.getroot() 12 13 for child in root: 14 # API name 和 API path 15 resources = child.iter("{http://eviware.com/soapui/config}resource") 16 for resource in resources: 17 APIName = resource.attrib["name"] 18 APIPath = resource.attrib["path"] 19 apiCount += 1 20 print("API Name:", APIName) 21 print("API path:", APIPath) 22 23 if "testSuite" in child.tag: 24 # testSuite 25 suiteName = child.attrib["name"] 26 suiteCount += 1 27 print("suiteName:", suiteName) 28 29 # testCase 30 cases = child.findall("{http://eviware.com/soapui/config}testCase") 31 for case in cases: 32 caseName = case.attrib["name"] 33 caseCount += 1 34 print("caseName:", caseName) 35 36 # testSetp 37 steps = case.findall("{http://eviware.com/soapui/config}testStep") 38 for step in steps: 39 stepName = step.attrib["name"] 40 stepCount += 1 41 print("stepName:", stepName) 42 43 # testAssert 44 assertions = case.iter("{http://eviware.com/soapui/config}assertion") 45 for assertion in assertions: 46 assertionName = assertion.attrib["name"] 47 assertCount += 1 48 print("assertionName:", assertionName) 49 50 print("----------------------------------") 51 print("apiCount:", apiCount) 52 print("suiteCount:", suiteCount) 53 print("caseCount:", caseCount) 54 print("stepCount:", stepCount) 55 print("assertCount:", assertCount) 56 57 58 if __name__ == ‘__main__‘: 59 xmlParse()
四、运行结果
可以看到API、TestSuite、TestCase、TestStep、Assertion等信息,解决了数据解析的问题。