tearDown not called when calling exit() in a unit test

Asked by Georgi Angelchov

Hi,

I have a .sikuli unit test that looks like this:

def setUp(self):
    print "Setting up..."
    #some set-up code goes here

def tearDown(self):
    print "Tearing Down..."
    #some code for tearing down goes here

def testSomething(self):
    if not exists("SomeImage.png"):
        print "Verification failed for SomeImage"
        exit(1)

The issue with the code above is that the tearDown method is not executed when the verification fails and the "exit(1)" line is hit. If I comment-out the "exit(1)" line than the tearDown method is called as expected.

Any hints about why tearDown is not called are highly appreciated :-)

I trued executing the test through both the Unit Test view in Sikuli IDE and through command-line with the same result.
The version I'm using is Sikuli X-1.0rc3 (r905)

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Georgi Angelchov (georgi-angelchov) said :
#1

Okay, it looks like the tearDown method is not invoked if my test fails for any reason (e.g., FindFailed). Seems like it's invoked only if my unit test script runs without an error.

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

If it was a unit test that makes sense, it should have looked this way:

def setUp(self):
    print "Setting up..."
    #some set-up code goes here

def tearDown(self):
    print "Tearing Down..."
    #some code for tearing down goes here

def testSomething(self):
    assert exists("SomeImage.png")

and if you want to produce a message in the test case:

def setUp(self):
    print "Setting up..."
    #some set-up code goes here

def tearDown(self):
    print "Tearing Down..."
    #some code for tearing down goes here

def testSomething(self):
    if not exists("SomeImage.png"):
        print "Verification failed for SomeImage"
        assert False
   else: assert true

the assert calls are what drives the workflow of a unit test and leads to the pass/fail/error stats:

setUp
test1
tearDown
setUp
test2
tearDown
setUp
test3
tearDown
setUp
test4
tearDown
....

But if you are really interested in unit testing: faq 1804

If your primary interest is NOT unit testing, just forget this and make normal scripts, that are run normally.

Revision history for this message
Georgi Angelchov (georgi-angelchov) said :
#3

Thanks for the answer.

Initially my test had asserts. I changed this to "print message and exit" because the AssertionError I was getting was not desrciptive in any way. I found the "print and exit" approach while searching how to make my test fail gracefully with a meaningfull message.

The "Print message and Assert" approach works as expected though, so thanks for pointing that out.

Thaks for the link to faq 1804 as well :)

Revision history for this message
Georgi Angelchov (georgi-angelchov) said :
#4

Thanks RaiMan, that solved my question.