unit testing: how to handle exceptions and continue in case of failures

Asked by Sonali

from sikuli import *
import OpenApplications
reload(OpenApplications)

dir = os.path.dirname(getBundlePath()) # the folder, where your script is stored
def setUp(self):
    logging.basicConfig(filename='..\\logfile.log', level=logging.DEBUG)
    Images.images()
def tearDown(self):
    OpenApplications.CloseApp()
    self.app.close()

class TestDemo(unittest.TestCase):

     def functiontest1(self):
  try:
   OpenApplications.OpenApp()
   print 'Applications should be opened successfully'
   click('Image1') ---> #This image is not present so program should not halt and continue to next step
   OpenApplications.CloseApp()
  except Exception as e:
   print (e)

  #Also program should execute this function even if it encounters some issues in above code
  def functiontest2(self):
        OpenApplications.OpenApp()
        print 'Applications should be opened successfully'
        OpenApplications.CloseApp()

suite = unittest.TestLoader().loadTestsFromTestCase(TestDemo)
outfile = open("..\\results.html", "w")
runner = HTMLTestRunner.HTMLTestRunner(stream=outfile, title='Test Report', description='Test Flows' )
runner.run(suite)
outfile.close()

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:

This question was reopened

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

use
if exists(img):
    click()
else:
    # do something else

... and follow the conventions and features of Python unit testing.

Revision history for this message
Sonali (sachchirp) said :
#3

Thanks Raiman. This is true when I know forhand which images will not be present and I can use if exists.
What for the images which may be not present at runtime , due to a bug in the code or any other reason.

As in, let's say if any of the images/steps in
functiontest1(self): OpenApplications.OpenApp() is incorrect then still it should catch that particular exception and continue rest of the program execution
OpenApplications.CloseApp() and also to next function in testdemo class
functiontest2(self)

OpenApplications.OpenApp() has series of actions. Some of them are as follows:

def OpenApp():
    click('Start')
    wait(1)
    click('Exceutive')
    wait(10)
    click('Control')
 etc...

class TestDemo(unittest.TestCase):

     def functiontest1(self):
  try:
   OpenApplications.OpenApp()
   print 'Applications should be opened successfully'
   OpenApplications.CloseApp()
  except Exception as e:
   print (e)

         def functiontest2(self):
                   OpenApplications.OpenApp()
                   print 'Applications should be opened successfully'
                  OpenApplications.CloseApp()

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

see comment #1

Revision history for this message
Sonali (sachchirp) said :
#5

Hi Raiman,

Comment #1

use
if exists(img):
    click()
else:
    # do something else

... and follow the conventions and features of Python unit testing.

That means I always have to use it for each image, there are series of image. Any other way like

try
  click(image1)

Revision history for this message
Sonali (sachchirp) said :
#6

try:
    click('Start')
    wait(1)
    click('Exceutive')
    wait(10)
    click('Control')
 etc...
except exception e:
 print 'No such images found'

But this does not work for me

Revision history for this message
Sonali (sachchirp) said :
#7

Hi Raiman,

Sorry to bother you again. But apart from exists(image) can I use try-catch exception. but somehow it did not work for me :(

try:
    click('Start')
    wait(1)
    click('Exceutive')
    wait(10)
    click('Control')
 etc...
except exception e:
 print 'No such images found'

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

as mentioned: you have to learn some basic Python.

--- except exception e:
is not valid (syntax error!)

--- even when using
except Exception as e: (which is correct)

this does not help, since FindFailed is not a subclass of Exception.

so either:

except:

or:

except FindFailed:

But as also mentioned:

--- using exists() is much more elegant and transparent

--- your Unittest setup is not according to the rules

so make your homework and come back after having really tracked down your problems.

Revision history for this message
Sonali (sachchirp) said :
#9

Thanks Raiman.

I am right now in the learning phase and still learning and understanding python+Sikuli.
Also I had another basic question. How will it work in case I have one unit test class in which multiple test cases(functions) are present and I want all of the test cases(functions) to be executed even though some functions end up in error.

class TestDemo(unittest.TestCase):

 def functiontest1(self):
 ....

 def functiontest2(self):
 ....

 def functiontest3(self):
 ....

suite = unittest.TestLoader().loadTestsFromTestCase(TestDemo)
outfile = open("..\\results.html", "w")
runner = HTMLTestRunner.HTMLTestRunner(stream=outfile, title='Test Report', description='Test Flows' )
runner.run(suite)
outfile.close()

Can you help with this problem?

Provide an answer of your own, or ask Sonali for more information if necessary.

To post a message you must log in.