Python Library Report
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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', ]
  122. if basepath is not None:
  123. target_handlers.append('logwarn')
  124. # define handler
  125. #
  126. if target == 'stdout':
  127. handler = dict(main={
  128. 'level': 'DEBUG',
  129. 'formatter': 'format',
  130. 'class': 'logging.StreamHandler',
  131. 'stream': 'ext://sys.stdout',
  132. })
  133. elif target == 'logfile':
  134. handler = dict(main={
  135. 'level': 'DEBUG',
  136. 'formatter': 'format',
  137. 'class': 'logging.handlers.RotatingFileHandler',
  138. 'filename': os.path.join(basepath, 'messages.log'),
  139. 'mode': 'a',
  140. 'maxBytes': 10485760,
  141. 'backupCount': 7
  142. })
  143. else:
  144. handler = dict(my_handler={
  145. 'level': 'DEBUG',
  146. 'formatter': 'my_format',
  147. 'class': 'logging.NullHandler',
  148. })
  149. if ring_logs is not None:
  150. target_handlers.append('ring')
  151. handler['ring'] = {
  152. 'class': 'report.collectingRingHandler',
  153. 'max_logs': ring_logs,
  154. }
  155. if basepath is not None:
  156. handler['logwarn'] = {
  157. 'level': 'WARNING',
  158. 'formatter': 'long',
  159. 'class': 'logging.handlers.RotatingFileHandler',
  160. 'filename': os.path.join(basepath, 'messages.warn'),
  161. 'mode': 'a',
  162. 'maxBytes': 10485760,
  163. 'backupCount': 2
  164. }
  165. # define loggers
  166. #
  167. loggers = {}
  168. for name, lvl in log_name_lvl:
  169. loggers[name] = {
  170. 'handlers': target_handlers,
  171. 'level': lvl,
  172. 'propagate': False
  173. }
  174. # configure logging
  175. #
  176. dictConfig(dict(
  177. version=1,
  178. formatters={
  179. 'long': {
  180. 'format': LONG_FMT
  181. },
  182. 'format': {
  183. 'format': fmt,
  184. },
  185. },
  186. handlers=handler,
  187. loggers=loggers,
  188. ))
  189. def stdoutLoggingConfigure(log_name_lvl=[], fmt=SHORT_FMT):
  190. appLoggingConfigure(None, 'stdout', log_name_lvl=log_name_lvl, fmt=fmt)
  191. class testSession(dict):
  192. KEY_NAME = 'name'
  193. KEY_FAILED_TESTS = 'number_of_failed_tests'
  194. KEY_POSSIBLY_FAILED_TESTS = 'number_of_possibly_failed_tests'
  195. KEY_SUCCESS_TESTS = 'number_of_successfull_tests'
  196. KEY_ALL_TESTS = 'number_of_tests'
  197. KEY_EXEC_LVL = 'testcase_execution_level'
  198. KEY_EXEC_NAMES = 'testcase_names'
  199. KEY_LVL_NAMES = 'level_names'
  200. KEY_TESTCASELIST = 'testcases'
  201. KEY_UID_LIST = 'uid_list_sorted'
  202. #
  203. DEFAULT_BASE_DATA = {
  204. KEY_NAME: 'Default Testsession name',
  205. KEY_FAILED_TESTS: 0,
  206. KEY_POSSIBLY_FAILED_TESTS: 0,
  207. KEY_FAILED_TESTS: 0,
  208. KEY_SUCCESS_TESTS: 0,
  209. KEY_ALL_TESTS: 0,
  210. KEY_EXEC_LVL: TCEL_FULL,
  211. KEY_EXEC_NAMES: TCEL_NAMES,
  212. }
  213. def __init__(self, module_names=[], **kwargs):
  214. dict.__init__(self, time_consumption=0.)
  215. self.__testcase__ = None
  216. self.__set_base_data__(**kwargs)
  217. self.__configure_logging__(module_names)
  218. def __set_base_data__(self, **kwargs):
  219. for key in set([key for key in self.DEFAULT_BASE_DATA.keys()] + [key for key in kwargs.keys()]):
  220. self[key] = kwargs.get(key, self.DEFAULT_BASE_DATA.get(key))
  221. self[self.KEY_TESTCASELIST] = {}
  222. self[self.KEY_UID_LIST] = []
  223. def __configure_logging__(self, module_names):
  224. #
  225. # Configure logging for testSession
  226. #
  227. logging_config = dict(
  228. version=1,
  229. formatters={
  230. 'short': {
  231. 'format': SHORT_FMT,
  232. },
  233. 'long': {
  234. 'format': LONG_FMT,
  235. },
  236. },
  237. handlers={
  238. 'console': {
  239. 'level': 'DEBUG',
  240. 'class': 'logging.NullHandler',
  241. 'formatter': 'short',
  242. },
  243. 'module_logs': {
  244. 'level': 'DEBUG',
  245. 'class': 'report.collectingHandler',
  246. 'formatter': 'short',
  247. },
  248. 'testcase_logs': {
  249. 'level': 'DEBUG',
  250. 'class': 'report.collectingTestcaseHandler',
  251. 'formatter': 'short',
  252. },
  253. },
  254. loggers=self.__module_loggers__(module_names),
  255. )
  256. dictConfig(logging_config)
  257. def __module_loggers__(self, module_names):
  258. rv = {}
  259. rv['__tLogger__'] = dict(handlers=['console', 'testcase_logs'], level='DEBUG', propagate=False)
  260. for name in module_names + ['__mLogger__']:
  261. rv[name] = dict(handlers=['console', 'module_logs'], level='DEBUG', propagate=False)
  262. return rv
  263. def testCase(self, name, testcase_execution_level, test_method, *args, **kwargs):
  264. if testcase_execution_level <= self[self.KEY_EXEC_LVL]:
  265. tLogger = logging.getLogger('__tLogger__')
  266. tHandler = collectingTestcaseHandler()
  267. if len(tHandler.MY_LOGS) > 0:
  268. raise AttributeError("Testcaselogger shall be empty after closing testcase!")
  269. tLogger._log(logging.DEBUG, name, None)
  270. if len(tHandler.MY_LOGS) != 1:
  271. raise AttributeError("Testcaselogger shall have only one entry for the main testcase (temporary)!")
  272. self.__testcase__ = tHandler.get_logs()[0]
  273. test_method(logging.getLogger('__tLogger__'), *args, **kwargs)
  274. self.__close_active_testcase__()
  275. def __close_active_testcase__(self):
  276. if self.__testcase__ is not None:
  277. name = self.__testcase__.get('message')
  278. #
  279. # Add testcase
  280. #
  281. tch = collectingTestcaseHandler()
  282. self.__testcase__['testcaseLogger'] = tch.get_logs()
  283. if name in self[self.KEY_TESTCASELIST]:
  284. raise AttributeError("Testcase named %s already exists" % name)
  285. self[self.KEY_TESTCASELIST][name] = self.__testcase__
  286. self[self.KEY_UID_LIST].append(name)
  287. #
  288. # Adapt testcase data
  289. #
  290. self[self.KEY_TESTCASELIST][name]['levelno'] = 0
  291. self[self.KEY_TESTCASELIST][name]['time_consumption'] = 0.
  292. for teststep in self[self.KEY_TESTCASELIST][name]['testcaseLogger']:
  293. # store maximum level to testcase
  294. if teststep.get('levelno') > self[self.KEY_TESTCASELIST][name]['levelno']:
  295. self[self.KEY_TESTCASELIST][name]['levelno'] = teststep.get('levelno')
  296. self[self.KEY_TESTCASELIST][name]['levelname'] = teststep.get('levelname')
  297. # store time_consumption for teststep
  298. try:
  299. teststep['time_consumption'] = teststep['created'] - teststep['moduleLogger'][-1]['created']
  300. except IndexError:
  301. teststep['time_consumption'] = 0.
  302. # Increment testcase time_comsumption
  303. # Increment testcase counters
  304. #
  305. self[self.KEY_ALL_TESTS] += 1
  306. if self[self.KEY_TESTCASELIST][name]['levelno'] <= logging.INFO:
  307. self[self.KEY_SUCCESS_TESTS] += 1
  308. elif self[self.KEY_TESTCASELIST][name]['levelno'] >= logging.ERROR:
  309. self[self.KEY_FAILED_TESTS] += 1
  310. else:
  311. self[self.KEY_POSSIBLY_FAILED_TESTS] += 1
  312. # Set testcase time and time_consumption
  313. self[self.KEY_TESTCASELIST][name]['time_start'] = self.__testcase__['asctime']
  314. self[self.KEY_TESTCASELIST][name]['time_finished'] = teststep['asctime']
  315. self[self.KEY_TESTCASELIST][name]['time_consumption'] = teststep['created'] - self.__testcase__['created']
  316. # Set testcase time consumption
  317. self['time_consumption'] += self[self.KEY_TESTCASELIST][name]['time_consumption']
  318. self.__testcase__ = None