Sikuli xmlrunner/htmlrunner with try catch in test cases

Asked by stewazy

Hello everyone, I want to aplogize to Raiman, I'm openning a new question related to xmlrunner/html runner.

First off I'm using this xml runner.I have a problem logging error in a file log.log and at the same time getting a correct xmlreport.xml report. When I have try to run my suite the logging works flawlessly but the xmlreport generates a positive report every time, regardless if my test fails.

Here is my test setup:

import unittest
import sys
import logging
import os
import traceback

from sikuli import *

class setUp(unittest.TestCase):
    def setUp(self):
        file = open("C:\\Sikuli\\log.log", 'a')
        file.write('Starting")
        file.close()

        wait(1)

    def tearDown(self):
        file = open("C:\\Sikuli\\log.log", 'a')
        file.write('Ending")
        file.close()

    def myTest(self):
        try:
            #test
        except:
            var = traceback.format_exc()
            file = open("C:\\Sikuli\\log.log", 'a')
            file.write(var)
            file.close()

Here is my runner setup

import sys
import os

if not helpersPath in sys.path: sys.path.append(helpersPath)
MyTestPath = "C:\\Sikuli\\Mytests"
if not MyTestPath in sys.path: sys.path.append(MyTestPath )

from xmlrunner import *
from sikuli.Sikuli import *
from MyTest import *

#setup - clean log file
fileName = "C:\\Sikuli\\log.log"
f = open(fileName,'w')
f.truncate()
f.close()

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

result = XMLTestRunner(file("C:\\Sikuli\\test-results.xml", "w")).run(suite)

Is there any way that I can get the error in my log file and at the same time to generate a working xmlresult file at the end ? Thank you in advance and especially for your patience with me :) I a python newbie :)

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
stewazy
Solved:
Last query:
Last reply:
Revision history for this message
stewazy (stewazy) said :
#1

https://drive.google.com/open?id=0B3yXqb4fPqMBTlJ5R0xYdTRrYWc here is the xmlrunner.py I forgot to link.

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

Since you are really ambitious and willing to learn ;-)

... here is a working example with all your aspects:

- I am using the bundled XMLTestRunner
- the log and xml files are written to the folder where the script is located
- everything in one script for demo only - you might push things to subscripts as needed (test cases, helpers, ...)
- to tell UnitTest (and hence XMLTestRunner) about test case success you have to use appropriate assert statements

from sikuli import *

import unittest
import logging
import traceback
import XMLTestRunner1

logFile = os.path.join(getParentPath(), "log.txt")
xmlFile = os.path.join(getParentPath(), "mytest.xml")

def logWrite(text):
        file = open(logFile, 'a')
        file.write(text + "\n")
        file.close()

def logReset():
        file = open(logFile, 'w')
        file.close()

class MyTest(unittest.TestCase):
    def setUp(self):
        logWrite("Starting")
        wait(1)

    def tearDown(self):
        logWrite("Ending")

    def test1(self):
        try:
            1/0 # gives runtime exception
        except:
            var = traceback.format_exc()
            logWrite(var)
            assert false

logReset()
suite = unittest.TestLoader().loadTestsFromTestCase(MyTest)
result = XMLTestRunner1.XMLTestRunner(file(xmlFile, "w")).run(suite)

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

BTW:
import os, sys, time

is not needed, since already imported by SikuliX

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

... and
from sikuli import *

should always be in the first line (not needed im main script)

Revision history for this message
stewazy (stewazy) said :
#5

My case still stand because I'm bound to 1.0.1 Sikuli on VM :(

Revision history for this message
stewazy (stewazy) said :
#6

Which xml runner are you using ?

Revision history for this message
stewazy (stewazy) said :
#7

Sorry for spamming, I didn't find an edit button my goal is to get the same message in the log.log and xmlreport file.

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

sorry, forgot about 1.0.1.

--- my goal is to get the same message in the log.log and xmlreport file.
I guess, that is not possible in an easy way.
So I think you should setup your own myAssert function, that internally takes care for this completeness of logging information and finally uses assert to signal to unitest/xmltestrunner.

--- Which xml runner are you using ?
is not important. use the one, that satisfies your needs and works with SikuliX. To get anything to the xml file you have to use assert as already mentioned. For further features you have to consult the Python unites docs and that of the test runner of your choice.

--- Sorry for spamming ...
no need ;-)
if you think, one of your comments is obsolete or unnecessary, just use the Hide feature.

Revision history for this message
stewazy (stewazy) said :
#9

OK, thanks @RaiMan, I will try to find a solution in the next week if I don't write that means i quit xD, If find a suitable solution I will post it here

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

ok, all the best.

At least you gave me some things to think about in version 2 ;-)

Revision history for this message
stewazy (stewazy) said :
#11

Or if you have any sugestions for me to write my own xml runner :D ?

Revision history for this message
stewazy (stewazy) said :
#12

import unittest
import sys
import logging
import os
import traceback

from sikuli import *

class TestClass (unittest.TestCase):
    def setUp(self):
        try:
           #do smth
        except:
           #handle error and raise same error
            raise

    def tearDown(self):
        try:
            #dosmth
        except:
            #handle error and raise same exception
            raise

    def testSingleTicket(self):
        try:
          #do smth
        except:
           #handle error and raise same error
            raise

I found a suitable solution for my problem. Here is the solution.