【发布时间】:2017-06-26 09:14:41
【问题描述】:
我正在从 soapui TESTS-TestSuites.xml 文件创建自定义报告。
下面是xml格式的结果——
<?xml version="1.0" encoding="UTF-8" ?>
<testsuites>
<testsuite errors="0" failures="0" id="0" name="APIs and API Versions retrieval" package="PAD API Regression" tests="11" time="1.535">
<testcase name="GET-APIs" time="0.942" />
<testcase name="GET-flight-inspiration-search-SearchnShoppingFamily" time="0.064" />
<testcase name="GET-top-flight-destinations_SearchnShoppingFamily" time="0.059" />
<testcase name="GET-flight-search-by-calendar_SearchnShoppingFamily" time="0.058" />
<testcase name="GET-top-flight-destinations_TravelIntelligenceFamily" time="0.064" />
<testcase name="GET-fare-search-history_TravelIntelligenceFamily" time="0.089" />
<testcase name="GET-checked-in-links_UtilitiesFamily" time="0.067" />
<testcase name="GET-nearest-relevant-airport_UtilitiesFamily" time="0.062" />
<testcase name="GET-airport-lists-by-city/country_UtilitiesFamily" time="0.054" />
<testcase name="GET-hotel-search_HotelAPIsFamily" time="0.052" />
<testcase name="GET-APIs_with_invalid_ID" time="0.024" />
</testsuite>
<testsuite errors="0" failures="1" id="1" name="Service and API Catalogue" package="PAD API Regression" tests="13" time="0.629">
<testcase name="GET-List_of_catalogues" time="0.058" />
<testcase name="GET-Catalogue_AirlineIT2" time="0.01">
<failure message="Cancelling due to failed test step" type="Cancelling due to failed test step">
<![CDATA[<h3><b>Catalogue_AirlineIT2 Failed</b></h3><pre>[Valid HTTP Status Codes] Response status code:200 is not in acceptable list of status codes
</pre><hr/>]]>
</failure>
</testcase>
<testcase name="GET-Catalogue_Distribution" time="0.05" />
<testcase name="GET-Catalogue_Self-service" time="0.035" />
<testcase name="GET-All_Families_of_Self-Service_Catalogue" time="0.065" />
<testcase name="GET-Families_Level_1_of_Self-Service_Catalogue" time="0.053" />
<testcase name="GET-Families_Level_2_of_Self-Service_Catalogue" time="0.058" />
<testcase name="GET-Family_Air API_of_Self-Service_Catalogue" time="0.03" />
<testcase name="GET-Family_Hotel API_of_Self-Service_Catalogue" time="0.067" />
<testcase name="GET-Family_Search_and_Shopping_from_Air_APIs_of_Self-Service_Catalogue" time="0.052" />
<testcase name="GET-Family_Travel Intelligence API_of_Self-Service_Catalogue" time="0.036" />
<testcase name="GET-Family_Utilities API_of_Self-Service_Catalogue" time="0.105" />
<testcase name="GETFamily_Search_and_Shopping_from_Hotel_API_of_Self-Service_Catalogue" time="0.01" />
</testsuite>
</testsuites>
下面是我读取和获取报告结果的代码 -
import os
import os.path
import codecs
import xml.etree.ElementTree
import xml.etree.ElementTree as ET
from BeautifulSoup import BeautifulSoup
from shutil import copyfile
data = open(r'H:\Scripts\report\TESTS-TestSuites.xml')
root = ET.parse(data).getroot()
projectName = root.find('testsuite').get('package')
totalTime = 0
totalTestCases = 0
totalTestFailure = 0
for testsuites in root.findall('testsuite'):
totalTestCases += int(testsuites.get('tests'))
totalTime += float(testsuites.get('time'))
totalTestFailure += int(testsuites.get('failures'))
successRate = "%.2f" % ((float(totalTestCases) - float(totalTestFailure))/float(totalTestCases)*100)
print "SuccessRate", successRate
# Get All Test Cases
for testcases in root.iter('testcase'):
print testcases.get('name')
# Get All Failed Test Cases
for testcases in root.getiterator():
if testcases.tag == 'failure':
print testcases.text
# Get All Failed TestSuites
for testsuites in root.findall('testsuite'):
if int(testsuites.get('failures')) !=0:
print "Failed TestSuite name", testsuites.get('name')
如何获取失败的TestCase 的名称。
如果我关注,我会得到None
for testsuites in root.findall('testsuite'):
if int(testsuites.get('failures')) !=0:
print "Failed TestSuite name", testsuites.get('name')
for testcases in root.getiterator():
if testcases.tag == 'failure':
print testcases.get('name')
【问题讨论】:
-
如果您的目标是创建 html 报告,则使用
apache-ant并提供上述 xml 作为输入。有关更多详细信息,请参见此处。 - stackoverflow.com/questions/12855740/… -
我查过了。我的目的是创建像 damage-control-report 这样的报告。 .请看这里 - 我的另一个问题 - stackoverflow.com/questions/44453012/… 请检查我的问题的以下部分。如果您有任何想法,那就太好了。
-
对python或maven没有太多想法。你介意使用
groovy来处理xml吗? -
绝对不是。最终,我已经在我的 soapui 项目中从您的 groovy 脚本中获得了帮助。 ;-) stackoverflow.com/questions/41700437/…
-
很高兴知道这一点。可能你投票有用的答案;)
标签: xml python-2.7 groovy soapui