Python Library Report
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

__init__.py 13KB

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