1

結果の概要ファイルを作成するために、python を使用して xml を解析しようとしています。以下は私のコードと xml のスニペットです。以下のように、いくつかのセクションが<test>あります。</test>

<test name="tst_case1">
  <prolog time="2013-01-18T14:41:09+05:30"/> 
  <verification name="VP5" file="D:/Squish/HMI_testing/tst_case1/test.py" type="properties" line="6"> 
    <result time="2013-01-18T14:41:10+05:30" type="PASS"> 
      <description>VP5: Object propertycomparisonof ':_QMenu_3.enabled'passed</description>        <description type="DETAILED">'false' and 'false' are equal</description> 
      <description type="object">:_QMenu_3</description> 
      <description type="property">enabled</description> 
      <description type="failedValue">false</description> 
    </result> 
  </verification>
  <epilog time="2013-01-18T14:41:11+05:30"/> 
</test>

私が取得したいのは、1 つの<test>セクションに PASS / FAIL がいくつあるかということです。

以下のコードでは、合計の合格/不合格を xml ファイルに出力していますが、各セクションの合格/不合格の数に興味があります。これを取り出す手順を誰か教えてくれませんか?

import sys
import xml.dom.minidom as XY

file = open("result.txt", "w")
tree = XY.parse('D:\\Squish\\squish results\\Results-On-2013-01-18_0241 PM.xml')
Test_name = tree.getElementsByTagName('test')
Test_status = tree.getElementsByTagName('result')
count_testname =0
passcount = 0
failcount = 0
Test_name_array = []
for my_Test_name in Test_name:
    count_testname = count_testname+1
    passcount = 0
    failcount = 0
    my_Test_name_final = my_Test_name.getAttribute('name')
    Test_name_array = my_Test_name_final
    if(count_testname > 1):
        print(my_Test_name_final)
        for my_Test_status in Test_status:
            my_Test_status_final = my_Test_status.getAttribute('type')
            if(my_Test_status_final == 'PASS'):
               passcount = passcount+1
            if(my_Test_status_final == 'FAIL'):
               failcount = failcount+1
            print(str(my_Test_status_final))
4

4 に答える 4

3

このタスクには minidom を使用しません。DOM API は非常に扱いにくく、冗長で、検索や照合には適していません。

Python ライブラリにはxml.etree.ElementTreeAPIも含まれています。代わりにそれを使用します。

from xml.etree import ElementTree as ET

tree = ET.parse(r'D:\Squish\squish results\Results-On-2013-01-18_0241 PM.xml')
tests = dict()

# Find all <test> elements with a <verification> child:
for test in tree.findall('.//test[verification]'):
    passed = len(test.findall(".//result[@type='PASS']"))
    failed = len(test.findall(".//result[@type='FAIL']"))
    tests[test.attrib['name']] = {'pass': passed, 'fail': failed}

上記のコードは、要素ごとに成功したテストと失敗したテストの数をカウントし、それらを要素の属性に<test>キー付けされた辞書に格納します。name<test>

上記のコードを Python 3.2 と、投稿した別の質問からの完全な XML ドキュメントでテストしたところ、次の結果が得られました。

{'tst_Setup_menu_2': {'fail': 0, 'pass': 8}}
于 2013-01-20T12:33:32.390 に答える
0

標準モジュールではありませんが、特に高速なXml解析などを実行したい場合は、 lxmlをインストールする価値があります。

あなたの結果の完全な例なしで、私はそれらがどのように見えるかを推測しました。

from lxml import etree

tree = etree.parse("results.xml")

count_result_type = etree.XPath("count(.//result[@type = $name])")

for test in tree.xpath("//test"):
    print test.attrib['name']
    print "\t# FAILS ", count_result_type(test, name="FAIL")
    print "\t# PASSES", count_result_type(test, name="PASS")

私はあなたのxmlの推測に反して次の実行を生成しました。これにより、何が起こっているのかがわかります。

tst_case1
    # FAILS  1.0
    # PASSES 1.0
tst_case0
    # FAILS  0.0
    # PASSES 1.0
tst_case2
    # FAILS  0.0
    # PASSES 1.0
tst_case3
    # FAILS  0.0
    # PASSES 1.0

私がlxmlについて気に入っているのは、 YMMVがどれほど表現力豊かであるかということです。

于 2013-01-21T18:43:21.433 に答える
0

投稿していただきありがとうございます。minidon を使用して動作させました。xml.etree.ElementTree を使用してどのように解決できるかをまだ知りたい

import sys
import xml.dom.minidom as XY

file = open("Result_Summary.txt", "w")
#tree = XY.parse('D:\\Squish\\squish results\\Results-On-2013-01-18_0241 PM.xml')
#print (str(sys.argv[1]))
tree = XY.parse(sys.argv[1])

Test_name = tree.getElementsByTagName('test')
count_testname =0

file.write('Test Name \t\t\t No:PASS\t\t\t No:FAIL\t \n\n')
for my_Test_name in Test_name:
    count_testname = count_testname+1
    my_Test_name_final = my_Test_name.getAttribute('name')
    if(count_testname > 1):
        #print(my_Test_name_final)
        file.write(my_Test_name_final)
        file.write('\t\t\t\t')
        my_Test_status = my_Test_name.getElementsByTagName('result')
        passcount = 0
        failcount = 0
        for my_Test_status_1 in my_Test_status:
            my_Test_status_final = my_Test_status_1.getAttribute('type')
            if(my_Test_status_final == 'PASS'):
               passcount = passcount+1
            if(my_Test_status_final == 'FAIL'):
               failcount = failcount+1
            #print(str(my_Test_status_final))
        file.write(str(passcount))
        #print(passcount)
        file.write('\t\t\t\t')
        file.write(str(failcount))
       # print(failcount)
        file.write('\n')

#print ('loop count: %d' %count_testname)
#print('PASS count: %s' %passcount)
#print('FAIL count: %s' %failcount)
file.close()
于 2013-01-21T06:33:46.123 に答える
0

Squish を使用しているようです。\examples\regressiontestingの下の squish フォルダーを確認する必要があります。xml2result2html.pyというファイルがあります。ここでは、squish テストの結果を html に変換する例を見つけることができます。

于 2013-07-09T11:41:42.430 に答える