Back to index

enigmail  1.4.3
mozunit.py
Go to the documentation of this file.
00001 from unittest import TextTestRunner as _TestRunner, TestResult as _TestResult
00002 import inspect
00003 
00004 '''Helper to make python unit tests report the way that the Mozilla
00005 unit test infrastructure expects tests to report.
00006 
00007 Usage:
00008 
00009 import unittest
00010 from mozunit import MozTestRunner
00011 
00012 if __name__ == '__main__':
00013     unittest.main(testRunner=MozTestRunner())
00014 '''
00015 
00016 class _MozTestResult(_TestResult):
00017     def __init__(self, stream, descriptions):
00018         _TestResult.__init__(self)
00019         self.stream = stream
00020         self.descriptions = descriptions
00021 
00022     def getDescription(self, test):
00023         if self.descriptions:
00024             return test.shortDescription() or str(test)
00025         else:
00026             return str(test)
00027 
00028     def addSuccess(self, test):
00029         _TestResult.addSuccess(self, test)
00030         filename = inspect.getfile(test.__class__)
00031         testname = test._testMethodName
00032         self.stream.writeln("TEST-PASS | %s | %s" % (filename, testname))
00033 
00034     def addError(self, test, err):
00035         _TestResult.addError(self, test, err)
00036         self.printFail(test, err)
00037 
00038     def addFailure(self, test, err):
00039         _TestResult.addFailure(self, test, err)
00040         self.printFail(test,err)
00041 
00042     def printFail(self, test, err):
00043         exctype, value, tb = err
00044         # Skip test runner traceback levels
00045         while tb and self._is_relevant_tb_level(tb):
00046             tb = tb.tb_next
00047         if not tb:
00048             self.stream.writeln("TEST-UNEXPECTED-FAIL | NO TRACEBACK |")
00049         _f, _ln, _t = inspect.getframeinfo(tb)[:3]
00050         self.stream.writeln("TEST-UNEXPECTED-FAIL | %s | line %d, %s: %s" % 
00051                             (_f, _ln, _t, value.message))
00052 
00053     def printErrorList(self):
00054         for test, err in self.errors:
00055             self.stream.writeln("ERROR: %s" % self.getDescription(test))
00056             self.stream.writeln("%s" % err)
00057 
00058 
00059 class MozTestRunner(_TestRunner):
00060     def _makeResult(self):
00061         return _MozTestResult(self.stream, self.descriptions)
00062     def run(self, test):
00063         result = self._makeResult()
00064         test(result)
00065         result.printErrorList()
00066         return result