Python Library Report
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

__init__.py 13KB

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