How to create xml report using xmltestrunner for test suite

Asked by Harry

How to create xml report using xmltestrunner for test suite.
Please help

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Harry
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1
Revision history for this message
Harry (iam-harishetty) said :
#2

this is my code.Let me know if i m doing something wrong
From this case blank xml page is getting generated
Please help

**********code******************

import unittest
import xmlrunner
import comres
from launchemulator import *
from phonevalidation import *
from invalidcode import *
from callme import *

class xmlreport(unittest.TestCase):
    def open_the_emulator_and_launch_the_securechatapp(self):
        try:
            emuobj = launchemulator()
            res=emuobj.emulator()
            if(res=="Fail"):
                self.fail()
        except:
            raise

    def phone_number_validations(self):
        try:
            phnobj = phonevalidation()
            res=phnobj.blank_number()
            if(res=="Fail"):
                self.fail()

            res1=phnobj.invalid_number(comres.inv_no)
            if(res1=="Fail"):
                self.fail()

            res2=phnobj.valid_number()
            if(res2=="Fail"):
                self.fail()

            res3=phnobj.popup_edit()
            if(res3=="Fail"):
                self.fail()

            res4=phnobj.pencil_edit()
            if(res4=="Fail"):
                self.fail()
        except:
            raise

    def putting_invalid_otp_code(self):
        try:
            invobj = invalidcode()
            res=invobj.inval_otpcode(comres.inv_otp)
            if(res=="Fail"):
                self.fail()
        except:
                raise

    def getting_otp_code_through_phone_call(self):
        try:
            callobj = callme()
            res=callobj.val_otpcode()
            if(res=="Fail"):
                self.fail()
        except:
            raise

def suite():
    suite.addTest(xmlreport('open_the_emulator_and_launch_the_securechatapp'))
    suite.addTest(xmlreport('phone_number_validations'))
    suite.addTest(xmlreport('putting_invalid_otp_code'))
    suite.addTest(xmlreport('getting_otp_code_through_phone_call'))
    return suite

suite = suite()

if __name__=='__main__':
    try:
        unittest.main()
    except SystemExit,inst:
        if inst.args[0] is True:
            raise

timestamp = time.strftime('%Y_%m_%d_%H_%M')
rname = 'Test_Report_'+timestamp+'.xml'
my_dir="path"

suite = unittest.TestLoader().loadTestsFromTestCase(xmlreport)

result = xmlrunner.XMLTestRunner(file(my_dir+rname,"w"))
result.run(suite)

Revision history for this message
RaiMan (raimund-hocke) said :
#3

# some code above --- ok

def suite():
    suite.addTest(xmlreport('open_the_emulator_and_launch_the_securechatapp'))
    suite.addTest(xmlreport('phone_number_validations'))
    suite.addTest(xmlreport('putting_invalid_otp_code'))
    suite.addTest(xmlreport('getting_otp_code_through_phone_call'))
    return suite

suite = suite()

timestamp = time.strftime('%Y_%m_%d_%H_%M')
rname = 'Test_Report_'+timestamp+'.xml'
my_dir="path"

result = xmlrunner.XMLTestRunner(file(my_dir+rname,"w")).run(suite)

Revision history for this message
Harry (iam-harishetty) said :
#4

I copied the code.
Now the error is getting as "AttributeError ( 'function' object has no attribute 'addTest' )"

Revision history for this message
RaiMan (raimund-hocke) said :
#5

uups, overseen.

# code before ok

def createSuite():
    suite = unittest.TestSuite();
    suite.addTest(xmlreport('open_the_emulator_and_launch_the_securechatapp'))
    suite.addTest(xmlreport('phone_number_validations'))
    suite.addTest(xmlreport('putting_invalid_otp_code'))
    suite.addTest(xmlreport('getting_otp_code_through_phone_call'))
    return suite

suite = createSuite()

# code after ok

Revision history for this message
Harry (iam-harishetty) said :
#6

The code ran successfully but blank xml report got generated in the given path.
Like html report, i want to generate xml report for all the suite as in the below image.

http://marcelog.github.io/articles/cth_surefire_example.png

I am able to generate html report for the following suites:
  suite.addTest(xmlreport('open_the_emulator_and_launch_the_securechatapp'))
  suite.addTest(xmlreport('phone_number_validations'))
  suite.addTest(xmlreport('putting_invalid_otp_code'))
  suite.addTest(xmlreport('getting_otp_code_through_phone_call'))

Please help me on this

Revision history for this message
Harry (iam-harishetty) said :
#7

I have written the following script But blank xml file is getting generated in the specified path.
Please tell me if anything is wrong in the code.

import unittest
from xmlrunner import *
import comres
from launchemulator import *
from phonevalidation import *
from invalidcode import *
from callme import *

class xmlreport(unittest.TestCase):
    def open_the_emulator_and_launch_the_securechatapp(self):
        try:
            emuobj = launchemulator()
            res=emuobj.emulator()
            if(res=="Fail"):
                self.fail()
        except:
            raise

    def phone_number_validations(self):
        try:
            phnobj = phonevalidation()
            res=phnobj.blank_number()
            if(res=="Fail"):
                self.fail()

            res1=phnobj.invalid_number(comres.inv_no)
            if(res1=="Fail"):
                self.fail()

            res2=phnobj.valid_number()
            if(res2=="Fail"):
                self.fail()

            res3=phnobj.popup_edit()
            if(res3=="Fail"):
                self.fail()

            res4=phnobj.pencil_edit()
            if(res4=="Fail"):
                self.fail()
        except:
            raise

    def putting_invalid_otp_code(self):
        try:
            invobj = invalidcode()
            res=invobj.inval_otpcode(comres.inv_otp)
            if(res=="Fail"):
                self.fail()
        except:
                raise

    def getting_otp_code_through_phone_call(self):
        try:
            callobj = callme()
            res=callobj.val_otpcode()
            if(res=="Fail"):
                self.fail()
        except:
            raise

def createSuite():
    suite = unittest.TestSuite()
    suite.addTest(xmlreport('open_the_emulator_and_launch_the_securechatapp'))
    suite.addTest(xmlreport('phone_number_validations'))
    suite.addTest(xmlreport('putting_invalid_otp_code'))
    suite.addTest(xmlreport('getting_otp_code_through_phone_call'))
    return suite

if __name__=='__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(xmlreport)

timestamp = time.strftime('%Y_%m_%d_%H_%M')
rname = 'Test_Report_'+timestamp+'.xml'
my_dir="D:\\Hariprasad\\xmlreport\\"

suite = createSuite()
result = XMLTestRunner(file(my_dir+rname,"w"))
result.run(suite)

Revision history for this message
RaiMan (raimund-hocke) said :
#8

if __name__=='__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(xmlreport)

do not use this with SikuliX

Revision history for this message
RaiMan (raimund-hocke) said :
#9

what version of XMLTestRunner are you using?
(look into the .py file)

Revision history for this message
RaiMan (raimund-hocke) said :
#10

this worked for me:

import unittest
import XMLTestRunner

class myTest(unittest.TestCase):

    def test1(self):
        assert True

suite = unittest.TestLoader().loadTestsFromTestCase(myTest)

XMLTestRunner.XMLTestRunner(file("/Users/rhocke/testOut.txt", "w")).run(suite)

# content of testOut.txt

<testsuite errors="0" failures="0" name="unittest.suite.TestSuite" tests="1" time="0.001">
  <testcase classname="__main__.myTest" name="test1" time="0.0000"></testcase>
  <system-out><![CDATA[]]></system-out>
  <system-err><![CDATA[]]></system-err>
</testsuite>

Revision history for this message
Harry (iam-harishetty) said :
#11

Thanks Raiman , it worked for me as well. I didn't changed any code.
The only concern was the xml report was not viewable on Safari browser. I was able to see the blank page.
Any reason why it's happening.

Revision history for this message
RaiMan (raimund-hocke) said :
#12

I put the above XML into a test.xml and when opening with Safari, it shows the content of the file correctly as XML tree.

Revision history for this message
Harry (iam-harishetty) said :
#13

My by default browser was safari for xml file.But when o tried to open using firefox , it shows the content of the file correctly as XML tree.