1

I am using the ElementTree library in python to parse an XML but unable to extract a field.

Parsing sample XML as shown below.

Here I want to check for failure and print corresponding testcase name.

From the below example I need to get output as featTest and featuTest

<?xml version='1.0' encoding='UTF-8' ?>
<testsuite name="" tests="5" failures="1" flakes="1" errors="0" skipped="0" time="339.777" hostname="localhost">
  <testcase name="featTest" classname="test.urlOpenState.id" time="25.874">
    <failure>java.lang.RuntimeException:
    </failure>
  </testcase>
  <testcase name="savTest" classname="com.State.id" time="2.886" />
  <testcase name="featuTest" classname="test.urlte.id" time="24.171" flaky="true">
    <failure>java.lang.RuntimeException:
    </failure>
  </testcase>
  <testcase name="feaTest" classname="test.urlte.id" time="4.645" />
  <testcase name="quiest" classname="test.urls.id" time="9.008" />
</testsuite>

link to guide or any suggestion will be helpful.

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
mebb
  • 123
  • 10
  • 1
    Can you add your Python code so we can help you figure out what you're doing wrong? – Daniel Haley Oct 23 '20 at 15:59
  • What exactly do you mean by "output as featTest and featuTest"? – Jack Fleeting Oct 23 '20 at 19:00
  • @JackFleeting i want to extract just the failed cases from the example.. here we have **testcase name="featTest"** and **testcase name="featuTest"** – mebb Oct 23 '20 at 19:09
  • How do you know that a testcase has failed - is it because they have a `name` attribute with an attribute value of either` featTest` or `featuTest` or is it something else? – Jack Fleeting Oct 23 '20 at 22:30
  • @JackFleeting following are the content from xml for failed testcases **` java.lang.RuntimeException: java.lang.RuntimeException: `** remaining ones ` ` are successfull testcases. – mebb Oct 24 '20 at 14:42
  • So xpath should be “`//testcase[failure]`”. If you add your Python it should be very easy to help you. – Daniel Haley Oct 24 '20 at 17:40

1 Answers1

0

I think I understand you know. Try this:

import xml.etree.ElementTree as ET

failure = """[your xml above]"""

doc = ET.fromstring(failure)
for f in doc.findall('.//testcase[failure]'):
     print(f.attrib['name'])

Output:

featTest
featuTest

Edit:

To extract the attribute values of the name attribute of test cases which did NOT fail, try this:

for f in doc.findall('.//testcase'):
    if not f.findall('.//failure'):
        print(f.attrib['name'])    

Output:

savTest
feaTest
quiest

Just FYI, the support of ElementTree for xpath is quite limited. If available to you, use lxml for that purpose. As you'll see below, it's much simpler, because the version of xpath supported by lxml includes the function not():

from lxml import etree
ldoc = etree.XML(failure.encode())
for case in ldoc.xpath('//testcase[not(failure)]/@name'):
    print(case)

Same output as above.

Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • thanks for the details it works fine. but do we have a way to print other testcase names i.e the successful ones ? from the above eg i want to print `feaTest quiest savTest` – mebb Oct 25 '20 at 08:18