Python Library Report
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

__init__.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. report (Report Module)
  6. ======================
  7. **Author:**
  8. * Dirk Alders <sudo-dirk@mount-mockery.de>
  9. **Description:**
  10. The Module is designed to help with python logging and to support some handlers for logging to memory.
  11. **Submodules:**
  12. * :class:`report.collectingHandler`
  13. * :class:`report.collectingRingHandler`
  14. * :class:`report.collectingTestcaseHandler`
  15. * :func:`report.consoleLoggingConfigure`
  16. * :class:`report.testSession`
  17. **Unittest:**
  18. See also the :download:`unittest <../../report/_testresults_/unittest.pdf>` documentation.
  19. """
  20. __DEPENDENCIES__ = []
  21. import collections
  22. import logging
  23. from logging.config import dictConfig
  24. import os
  25. import sys
  26. logger_name = 'REPORT'
  27. logger = logging.getLogger(logger_name)
  28. __DESCRIPTION__ = """The Module {\\tt %s} is designed to help with python logging and to support some handlers for logging to memory.
  29. For more Information read the sphinx documentation.""" % __name__.replace('_', '\_')
  30. """The Module Description"""
  31. __INTERPRETER__ = (2, 3, )
  32. """The Tested Interpreter-Versions"""
  33. SHORT_FMT = "%(asctime)s: %(name)s - %(levelname)s - %(message)s"
  34. """ A short formatter including the most important information"""
  35. LONG_FMT = """~~~~(%(levelname)-10s)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  36. File "%(pathname)s", line %(lineno)d, in %(funcName)s
  37. %(asctime)s: %(name)s- %(message)s
  38. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""
  39. """ A long formatter which results in links to the source code inside Eclipse"""
  40. MAX_FMT = """
  41. %(name)s
  42. %(levelno)s
  43. %(levelname)s
  44. %(pathname)s
  45. %(filename)s
  46. %(module)s
  47. %(lineno)d
  48. %(funcName)s
  49. %(created)f
  50. %(asctime)s
  51. %(msecs)d
  52. %(relativeCreated)d
  53. %(thread)d
  54. %(threadName)s
  55. %(process)d
  56. %(message)s"""
  57. DEFAULT_FMT = LONG_FMT
  58. """The default formatstring"""
  59. class collectingHandler(logging.Handler):
  60. MY_LOGS = []
  61. def __init__(self):
  62. logging.Handler.__init__(self)
  63. self.setFormatter(logging.Formatter(MAX_FMT))
  64. self.setLevel(logging.DEBUG)
  65. def emit(self, record):
  66. self.format(record)
  67. self.MY_LOGS.append(record.__dict__)
  68. def make_independent(self):
  69. self.MY_LOGS = []
  70. def get_logs(self):
  71. rv = []
  72. while len(self.MY_LOGS) > 0:
  73. rv.append(self.MY_LOGS.pop(0))
  74. return rv
  75. def get_str(self, logs=None, fmt=SHORT_FMT):
  76. logs = logs or self.MY_LOGS
  77. return '\n'.join([fmt % log for log in logs])
  78. def __len__(self):
  79. return len(self.MY_LOGS)
  80. def __str__(self):
  81. return self.get_str(self.MY_LOGS)
  82. class collectingRingHandler(collectingHandler):
  83. MY_LOGS = collections.deque([], 10)
  84. def __init__(self, max_logs=None):
  85. collectingHandler.__init__(self)
  86. if max_logs is not None and max_logs != self.MY_LOGS.maxlen:
  87. self.MY_LOGS.__init__(list(self.MY_LOGS), max_logs)
  88. def make_independent(self):
  89. self.MY_LOGS = collections.deque([], self.MY_LOGS.maxlen)
  90. def get_logs(self):
  91. return list(self.MY_LOGS)
  92. TCEL_SINGLE = 0
  93. """Testcase level (smoke), this is just a rough test for the main functionality"""
  94. TCEL_SMOKE = 10
  95. """Testcase level (smoke), this is just a rough test for the main functionality"""
  96. TCEL_SHORT = 50
  97. """Testcase level (short), this is a short test for an extended functionality"""
  98. TCEL_FULL = 90
  99. """Testcase level (full), this is a complete test for the full functionality"""
  100. TCEL_NAMES = {
  101. TCEL_SINGLE: 'Single Test',
  102. TCEL_SMOKE: 'Smoke Test (Minumum subset)',
  103. TCEL_SHORT: 'Short Test (Subset)',
  104. TCEL_FULL: 'Full Test (all defined tests)'
  105. }
  106. """Dictionary for resolving the test case levels (TCL) to a (human readable) name"""
  107. TCEL_REVERSE_NAMED = {
  108. 'short': TCEL_SHORT,
  109. 'smoke': TCEL_SMOKE,
  110. 'single': TCEL_SINGLE,
  111. 'full': TCEL_FULL,
  112. }
  113. """Dictionary for resolving named test case levels (TCL) to test case level number"""
  114. class collectingTestcaseHandler(collectingHandler):
  115. MY_LOGS = []
  116. def emit(self, record):
  117. self.format(record)
  118. self.MY_LOGS.append(record.__dict__)
  119. self.MY_LOGS[-1]['moduleLogger'] = collectingHandler().get_logs()
  120. def appLoggingConfigure(basepath, target, log_name_lvl=[], fmt=SHORT_FMT, ring_logs=None):
  121. target_handlers = ['main', 'logwarn']
  122. # define handler
  123. #
  124. if target == 'stdout':
  125. handler = dict(main={
  126. 'level': 'DEBUG',
  127. 'formatter': 'format',
  128. 'class': 'logging.StreamHandler',
  129. 'stream': 'ext://sys.stdout',
  130. })
  131. elif target == 'logfile':
  132. handler = dict(main={
  133. 'level': 'DEBUG',
  134. 'formatter': 'format',
  135. 'class': 'logging.handlers.RotatingFileHandler',
  136. 'filename': os.path.join(basepath, 'messages.log'),
  137. 'mode': 'a',
  138. 'maxBytes': 10485760,
  139. 'backupCount': 7
  140. })
  141. else:
  142. handler = dict(my_handler={
  143. 'level': 'DEBUG',
  144. 'formatter': 'my_format',
  145. 'class': 'logging.NullHandler',
  146. })
  147. if ring_logs is not None:
  148. target_handlers.append('ring')
  149. handler['ring'] = {
  150. 'class': 'report.collectingRingHandler',
  151. 'max_logs': ring_logs,
  152. }
  153. handler['logwarn'] = {
  154. 'level': 'WARNING',
  155. 'formatter': 'long',
  156. 'class': 'logging.handlers.RotatingFileHandler',
  157. 'filename': os.path.join(basepath, 'messages.warn'),
  158. 'mode': 'a',
  159. 'maxBytes': 10485760,
  160. 'backupCount': 2
  161. }
  162. # define loggers
  163. #
  164. loggers = {}
  165. for name, lvl in log_name_lvl:
  166. loggers[name] = {
  167. 'handlers': target_handlers,
  168. 'level': lvl,
  169. 'propagate': False
  170. }
  171. # configure logging
  172. #
  173. dictConfig(dict(
  174. version=1,
  175. formatters={
  176. 'long': {
  177. 'format': LONG_FMT
  178. },
  179. 'format': {
  180. 'format': fmt,
  181. },
  182. },
  183. handlers=handler,
  184. loggers=loggers,
  185. ))
  186. class testSession(dict):
  187. KEY_NAME = 'name'
  188. KEY_FAILED_TESTS = 'number_of_failed_tests'
  189. KEY_POSSIBLY_FAILED_TESTS = 'number_of_possibly_failed_tests'
  190. KEY_SUCCESS_TESTS = 'number_of_successfull_tests'
  191. KEY_ALL_TESTS = 'number_of_tests'
  192. KEY_EXEC_LVL = 'testcase_execution_level'
  193. KEY_EXEC_NAMES = 'testcase_names'
  194. KEY_LVL_NAMES = 'level_names'
  195. KEY_TESTCASELIST = 'testcases'
  196. KEY_UID_LIST = 'uid_list_sorted'
  197. #
  198. DEFAULT_BASE_DATA = {
  199. KEY_NAME: 'Default Testsession name',
  200. KEY_FAILED_TESTS: 0,
  201. KEY_POSSIBLY_FAILED_TESTS: 0,
  202. KEY_FAILED_TESTS: 0,
  203. KEY_SUCCESS_TESTS: 0,
  204. KEY_ALL_TESTS: 0,
  205. KEY_EXEC_LVL: TCEL_FULL,
  206. KEY_EXEC_NAMES: TCEL_NAMES,
  207. }
  208. def __init__(self, module_names=[], **kwargs):
  209. dict.__init__(self, time_consumption=0.)
  210. self.__testcase__ = None
  211. self.__set_base_data__(**kwargs)
  212. self.__configure_logging__(module_names)
  213. def __set_base_data__(self, **kwargs):
  214. for key in set([key for key in self.DEFAULT_BASE_DATA.keys()] + [key for key in kwargs.keys()]):
  215. self[key] = kwargs.get(key, self.DEFAULT_BASE_DATA.get(key))
  216. self[self.KEY_TESTCASELIST] = {}
  217. self[self.KEY_UID_LIST] = []
  218. def __configure_logging__(self, module_names):
  219. #
  220. # Configure logging for testSession
  221. #
  222. logging_config = dict(
  223. version=1,
  224. formatters={
  225. 'short': {
  226. 'format': SHORT_FMT,
  227. },
  228. 'long': {
  229. 'format': LONG_FMT,
  230. },
  231. },
  232. handlers={
  233. 'console': {
  234. 'level': 'DEBUG',
  235. 'class': 'logging.NullHandler',
  236. 'formatter': 'short',
  237. },
  238. 'module_logs': {
  239. 'level': 'DEBUG',
  240. 'class': 'report.collectingHandler',
  241. 'formatter': 'short',
  242. },
  243. 'testcase_logs': {
  244. 'level': 'DEBUG',
  245. 'class': 'report.collectingTestcaseHandler',
  246. 'formatter': 'short',
  247. },
  248. },
  249. loggers=self.__module_loggers__(module_names),
  250. )
  251. dictConfig(logging_config)
  252. def __module_loggers__(self, module_names):
  253. rv = {}
  254. rv['__tLogger__'] = dict(handlers=['console', 'testcase_logs'], level='DEBUG', propagate=False)
  255. for name in module_names + ['__mLogger__']:
  256. rv[name] = dict(handlers=['console', 'module_logs'], level='DEBUG', propagate=False)
  257. return rv
  258. def testCase(self, name, testcase_execution_level, test_method, *args, **kwargs):
  259. if testcase_execution_level <= self[self.KEY_EXEC_LVL]:
  260. tLogger = logging.getLogger('__tLogger__')
  261. tHandler = collectingTestcaseHandler()
  262. if len(tHandler.MY_LOGS) > 0:
  263. raise AttributeError("Testcaselogger shall be empty after closing testcase!")
  264. tLogger._log(logging.DEBUG, name, None)
  265. if len(tHandler.MY_LOGS) != 1:
  266. raise AttributeError("Testcaselogger shall have only one entry for the main testcase (temporary)!")
  267. self.__testcase__ = tHandler.get_logs()[0]
  268. test_method(logging.getLogger('__tLogger__'), *args, **kwargs)
  269. self.__close_active_testcase__()
  270. def __close_active_testcase__(self):
  271. if self.__testcase__ is not None:
  272. name = self.__testcase__.get('message')
  273. #
  274. # Add testcase
  275. #
  276. tch = collectingTestcaseHandler()
  277. self.__testcase__['testcaseLogger'] = tch.get_logs()
  278. if name in self[self.KEY_TESTCASELIST]:
  279. raise AttributeError("Testcase named %s already exists" % name)
  280. self[self.KEY_TESTCASELIST][name] = self.__testcase__
  281. self[self.KEY_UID_LIST].append(name)
  282. #
  283. # Adapt testcase data
  284. #
  285. self[self.KEY_TESTCASELIST][name]['levelno'] = 0
  286. self[self.KEY_TESTCASELIST][name]['time_consumption'] = 0.
  287. for teststep in self[self.KEY_TESTCASELIST][name]['testcaseLogger']:
  288. # store maximum level to testcase
  289. if teststep.get('levelno') > self[self.KEY_TESTCASELIST][name]['levelno']:
  290. self[self.KEY_TESTCASELIST][name]['levelno'] = teststep.get('levelno')
  291. self[self.KEY_TESTCASELIST][name]['levelname'] = teststep.get('levelname')
  292. # store time_consumption for teststep
  293. try:
  294. teststep['time_consumption'] = teststep['created'] - teststep['moduleLogger'][-1]['created']
  295. except IndexError:
  296. teststep['time_consumption'] = 0.
  297. # Increment testcase time_comsumption
  298. # Increment testcase counters
  299. #
  300. self[self.KEY_ALL_TESTS] += 1
  301. if self[self.KEY_TESTCASELIST][name]['levelno'] <= logging.INFO:
  302. self[self.KEY_SUCCESS_TESTS] += 1
  303. elif self[self.KEY_TESTCASELIST][name]['levelno'] >= logging.ERROR:
  304. self[self.KEY_FAILED_TESTS] += 1
  305. else:
  306. self[self.KEY_POSSIBLY_FAILED_TESTS] += 1
  307. # Set testcase time and time_consumption
  308. self[self.KEY_TESTCASELIST][name]['time_start'] = self.__testcase__['asctime']
  309. self[self.KEY_TESTCASELIST][name]['time_finished'] = teststep['asctime']
  310. self[self.KEY_TESTCASELIST][name]['time_consumption'] = teststep['created'] - self.__testcase__['created']
  311. # Set testcase time consumption
  312. self['time_consumption'] += self[self.KEY_TESTCASELIST][name]['time_consumption']
  313. self.__testcase__ = None