← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #177954]: Can sikuli take a screenshot on failure / error? --- rc3: currently no

 

Question #177954 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/177954

RaiMan posted a new comment:
Motivated by your idea, I found a neat solution, that might extend the
support for unit testing through Sikuli "dramatically".

This is a generalized solution, that allows to make and store a
screenshot, in case a test function fails, exactly in the same moment.

You do not have to touch your TestCase's code.

Based on the documentation at:
http://docs.python.org/library/unittest.html

--- a basic all in one template (e.g. mainTest.sikuli):

import unittest

class TestSik(unittest.TestCase):
    def setUp(self):
        print "\n*** in setUp"
    def test1(self):
        print "*** in Test1"
        assert False
    def test2(self):
        print "*** in Test2"
        assert True
    def tearDown(self):
        print "*** in tearDown"

class myTestResult(unittest.TestResult):
    def addFailure(self, test, err):
        print "\n*** from addFailure",test, err
        unittest.TestResult.addFailure(self,test,err)

tr = myTestResult()

suite = unittest.TestLoader().loadTestsFromTestCase(TestSik)
suite.run(tr)

# did not find out yet, how to use TextTestRunner
# to get some readable output from myTestResult instance
print "*** Test summary ----------------"
print "Tests run:", tr.testsRun
if tr.wasSuccessful():
    print "no Failures"
    exit(0)
print "Failures:", len(tr.failures)
print "--------------"
for e in tr.failures: 
    print "*** FAIL:", e[0]        
    x = e[1].split("\n")
    x1 = " ".join(x[1].split(",")[1:3])
    print "%s ( %s )"%(x1, x[2].strip())
    print "--------------"
exit(1)

The trick is to subclass TestResult and overwrite the respective method (in this case addFailure, which is called when a test fails).
Here we can do, whatever we want and finally go back to the original method, to let TestResult do its job.

This might be further packed in a utility class/module, that can be used
all over the place.

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.