run.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/usr/bin/python -O
  2. import curses
  3. import importlib
  4. import sys
  5. import unittest
  6. test_order = [
  7. 'qubes.tests.events',
  8. 'qubes.tests.vm.init',
  9. 'qubes.tests.vm.qubesvm',
  10. 'qubes.tests.init'
  11. ]
  12. sys.path.insert(1, '../../')
  13. class ANSIColor(dict):
  14. def __init__(self):
  15. super(ANSIColor, self).__init__()
  16. try:
  17. curses.setupterm()
  18. except curses.error:
  19. return
  20. self['black'] = curses.tparm(curses.tigetstr('setaf'), 0)
  21. self['red'] = curses.tparm(curses.tigetstr('setaf'), 1)
  22. self['green'] = curses.tparm(curses.tigetstr('setaf'), 2)
  23. self['yellow'] = curses.tparm(curses.tigetstr('setaf'), 3)
  24. self['blue'] = curses.tparm(curses.tigetstr('setaf'), 4)
  25. self['magenta'] = curses.tparm(curses.tigetstr('setaf'), 5)
  26. self['cyan'] = curses.tparm(curses.tigetstr('setaf'), 6)
  27. self['white'] = curses.tparm(curses.tigetstr('setaf'), 7)
  28. self['bold'] = curses.tigetstr('bold')
  29. self['normal'] = curses.tigetstr('sgr0')
  30. def __missing__(self, key):
  31. return ''
  32. class ANSITestResult(unittest.TestResult):
  33. '''A test result class that can print colourful text results to a stream.
  34. Used by TextTestRunner. This is a lightly rewritten unittest.TextTestResult.
  35. '''
  36. separator1 = unittest.TextTestResult.separator1
  37. separator2 = unittest.TextTestResult.separator2
  38. def __init__(self, stream, descriptions, verbosity):
  39. super(ANSITestResult, self).__init__(stream, descriptions, verbosity)
  40. self.stream = stream
  41. self.showAll = verbosity > 1
  42. self.dots = verbosity == 1
  43. self.descriptions = descriptions
  44. self.color = ANSIColor()
  45. def _fmtexc(self, err):
  46. s = str(err[1])
  47. if s:
  48. return '{color[bold]}{}:{color[normal]} {!s}'.format(
  49. err[0].__name__, err[1], color=self.color)
  50. else:
  51. return '{color[bold]}{}{color[normal]}'.format(
  52. err[0].__name__, color=self.color)
  53. def getDescription(self, test):
  54. teststr = str(test).split('/')
  55. teststr[-1] = '{color[bold]}{}{color[normal]}'.format(
  56. teststr[-1], color=self.color)
  57. teststr = '/'.join(teststr)
  58. doc_first_line = test.shortDescription()
  59. if self.descriptions and doc_first_line:
  60. return '\n'.join((teststr, ' {}'.format(
  61. doc_first_line, color=self.color)))
  62. else:
  63. return teststr
  64. def startTest(self, test):
  65. super(ANSITestResult, self).startTest(test)
  66. if self.showAll:
  67. self.stream.write(self.getDescription(test))
  68. self.stream.write(' ... ')
  69. self.stream.flush()
  70. def addSuccess(self, test):
  71. super(ANSITestResult, self).addSuccess(test)
  72. if self.showAll:
  73. self.stream.writeln('{color[green]}ok{color[normal]}'.format(
  74. color=self.color))
  75. elif self.dots:
  76. self.stream.write('.')
  77. self.stream.flush()
  78. def addError(self, test, err):
  79. super(ANSITestResult, self).addError(test, err)
  80. if self.showAll:
  81. self.stream.writeln(
  82. '{color[red]}{color[bold]}ERROR{color[normal]} ({})'.format(
  83. self._fmtexc(err), color=self.color))
  84. elif self.dots:
  85. self.stream.write(
  86. '{color[red]}{color[bold]}E{color[normal]}'.format(
  87. color=self.color))
  88. self.stream.flush()
  89. def addFailure(self, test, err):
  90. super(ANSITestResult, self).addFailure(test, err)
  91. if self.showAll:
  92. self.stream.writeln('{color[red]}FAIL{color[normal]}'.format(
  93. color=self.color))
  94. elif self.dots:
  95. self.stream.write('{color[red]}F{color[normal]}'.format(
  96. color=self.color))
  97. self.stream.flush()
  98. def addSkip(self, test, reason):
  99. super(ANSITestResult, self).addSkip(test, reason)
  100. if self.showAll:
  101. self.stream.writeln(
  102. '{color[cyan]}skipped{color[normal]} ({})'.format(
  103. reason, color=self.color))
  104. elif self.dots:
  105. self.stream.write('{color[cyan]}s{color[normal]}'.format(
  106. color=self.color))
  107. self.stream.flush()
  108. def addExpectedFailure(self, test, err):
  109. super(ANSITestResult, self).addExpectedFailure(test, err)
  110. if self.showAll:
  111. self.stream.writeln(
  112. '{color[yellow]}expected failure{color[normal]}'.format(
  113. color=self.color))
  114. elif self.dots:
  115. self.stream.write('{color[yellow]}x{color[normal]}'.format(
  116. color=self.color))
  117. self.stream.flush()
  118. def addUnexpectedSuccess(self, test):
  119. super(ANSITestResult, self).addUnexpectedSuccess(test)
  120. if self.showAll:
  121. self.stream.writeln(
  122. '{color[yellow]}{color[bold]}unexpected success{color[normal]}'.format(
  123. color=self.color))
  124. elif self.dots:
  125. self.stream.write(
  126. '{color[yellow]}{color[bold]}u{color[normal]}'.format(
  127. color=self.color))
  128. self.stream.flush()
  129. def printErrors(self):
  130. if self.dots or self.showAll:
  131. self.stream.writeln()
  132. self.printErrorList(
  133. '{color[red]}{color[bold]}ERROR{color[normal]}'.format(
  134. color=self.color),
  135. self.errors)
  136. self.printErrorList(
  137. '{color[red]}FAIL{color[normal]}'.format(color=self.color),
  138. self.failures)
  139. def printErrorList(self, flavour, errors):
  140. for test, err in errors:
  141. self.stream.writeln(self.separator1)
  142. self.stream.writeln('%s: %s' % (flavour,self.getDescription(test)))
  143. self.stream.writeln(self.separator2)
  144. self.stream.writeln('%s' % err)
  145. def demo(verbosity=2):
  146. import qubes.tests
  147. class TC_Demo(qubes.tests.QubesTestCase):
  148. '''Demo class'''
  149. def test_0_success(self):
  150. '''Demo test (success)'''
  151. pass
  152. def test_1_error(self):
  153. '''Demo test (error)'''
  154. raise Exception()
  155. def test_2_failure(self):
  156. '''Demo test (failure)'''
  157. self.fail('boo')
  158. def test_3_skip(self):
  159. '''Demo test (skipped by call to self.skipTest())'''
  160. self.skipTest('skip')
  161. @unittest.skip(None)
  162. def test_4_skip_decorator(self):
  163. '''Demo test (skipped by decorator)'''
  164. pass
  165. @unittest.expectedFailure
  166. def test_5_expected_failure(self):
  167. '''Demo test (expected failure)'''
  168. self.fail()
  169. @unittest.expectedFailure
  170. def test_6_unexpected_success(self):
  171. '''Demo test (unexpected success)'''
  172. pass
  173. suite = unittest.TestLoader().loadTestsFromTestCase(TC_Demo)
  174. runner = unittest.TextTestRunner(stream=sys.stdout, verbosity=verbosity)
  175. runner.resultclass = ANSITestResult
  176. return runner.run(suite).wasSuccessful()
  177. def main():
  178. suite = unittest.TestSuite()
  179. loader = unittest.TestLoader()
  180. for modname in test_order:
  181. module = importlib.import_module(modname)
  182. suite.addTests(loader.loadTestsFromModule(module))
  183. runner = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
  184. runner.resultclass = ANSITestResult
  185. return runner.run(suite).wasSuccessful()
  186. if __name__ == '__main__':
  187. sys.exit(not main())