Python Library Report
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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