0

what would be the best strategy to compare following test reports (xmls):

test report one:

-<testReport>
-<Configuration>
   <TestProjectFile>D:\setting.config</TestProjectFile>
</Configuration>
-<Cases>
   -<Case>
       <Type>typeOne</Type>
       <ProjectName>Project1</ProjectName>
       <CaseName>Case1</CaseName>
   -</Case>
   -<Case>
       <Type>typeOne</Type>
       <ProjectName>Project1</ProjectName>
       <CaseName>Case2</CaseName>
   -</Case>
-</Cases>
</testReport>

test report two:

-<testReport>
-<Configuration>
   <TestProjectFile>D:\setting.config</TestProjectFile>
</Configuration>
-<Cases>
   -<Case>
       <Type>typeOne</Type>
       <ProjectName>Project1</ProjectName>
       <CaseName>Case1</CaseName>
   -</Case>
   -<Case>
       <Type>typeOne</Type>
       <ProjectName>Project1</ProjectName>
       <CaseName>Case3</CaseName>
   -</Case>
-</Cases>
</testReport>

I would like to compare two reports by CaseName and add Case(s) which are not in common between two report inside Out.xml file.

Since I am a newbie in python, I tried following code, but this is only gets the test which are in report1 but not in report2.

def compare( res1, res2, out ):    
    tree1 = ET.parse(res1)
    root1 = tree1.getroot()

    tree2 = ET.parse(res2)
    root2 = tree2.getroot()

    number = 0
    for child in root1:
        if child.tag == 'Cases':
            for Cases in child.findall('Case'):
                cname = Cases.find('CaseName')
                for child2 in root2:
                    if child2.tag == 'Cases':
                        for Cases2 in child2.findall('Case'):
                            cname2 = Cases2.find('CaseName')
                            if  cname.text == cname2.text:
                                child.remove(Cases)
    tree1.write(out)

compare('report1.xml', 'report2.xml', 'out.xml')
H'H
  • 1,638
  • 1
  • 15
  • 39
  • Don't copy XML from Internet Explorer. Copy the source code itself please. (I'm assuming that's where the `-` come from. I'm pretty sure they are not part of the actual XML) – Tomalak Nov 28 '16 at 10:29
  • There are many more questions like this: http://stackoverflow.com/search?q=%5Bpython%5D+%5Bxml%5D+compare - Please search first next time. – Tomalak Nov 28 '16 at 10:32
  • @Tomalak ur opinion is not related the concept of question. I wrote that I am newbie in python and xml. and for ur second comment, I could not find any similar question. there are lots of question for comparing trees, but I could not find any one similar to my question. – H'H Nov 28 '16 at 10:37
  • @Tomalak why did you marked it as duplicated? it is not. – H'H Nov 28 '16 at 10:43
  • (This is not a chat. We have enough time to write full-length words here.) You want to compare two XML files in Python. I've linked to many answers that compare XML files in Python. Just because you cannot directly copy and paste from any of them that does not mean that this isn't a duplicate. – Tomalak Nov 28 '16 at 10:55

0 Answers0