SoapUI xml檔案解析
阿新 • • 發佈:2021-06-18
一、背景
使用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等資訊,解決了資料解析的問題。