Added unittests for logging levels
This commit is contained in:
parent
09a3fb166f
commit
45154950ee
@ -1,4 +1,5 @@
|
||||
import os
|
||||
import shutil
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
from string import Template
|
||||
@ -36,6 +37,10 @@ class LoggerTest(unittest.TestCase):
|
||||
|
||||
self._services.add_singleton(Logger, self._log_settings, self._time_format_settings, self._app_host)
|
||||
|
||||
def tearDown(self):
|
||||
if os.path.isdir(self._log_settings.path):
|
||||
shutil.rmtree(self._log_settings.path)
|
||||
|
||||
def _check_general_requirements(self):
|
||||
self.assertIsNotNone(self._services)
|
||||
self.assertIsNotNone(self._log_settings)
|
||||
@ -98,3 +103,114 @@ class LoggerTest(unittest.TestCase):
|
||||
|
||||
self.assertGreater(len(log_content), 0)
|
||||
self.assertTrue(log_content[len(log_content) - 1].endswith(f'[ TRACE ] [ {__name__} ]: {__name__}.test_trace:\n'))
|
||||
|
||||
def test_debug(self):
|
||||
print(f'{__name__}.test_debug:')
|
||||
logger: Logger = self._services.get_service(LoggerBase)
|
||||
logger.create()
|
||||
logger.debug(__name__, f'{__name__}.test_debug:')
|
||||
|
||||
log_file = Template(self._log_settings.filename).substitute(
|
||||
date_time_now=self._app_host.date_time_now.strftime(self._time_format_settings.date_time_format),
|
||||
start_time=self._app_host.start_time.strftime(self._time_format_settings.date_time_log_format)
|
||||
)
|
||||
log_content = []
|
||||
|
||||
try:
|
||||
with open(self._log_settings.path + log_file, "r") as log:
|
||||
log_content = log.readlines()
|
||||
log.close()
|
||||
except Exception as e:
|
||||
print('Cannot open log file', e)
|
||||
|
||||
self.assertGreater(len(log_content), 0)
|
||||
self.assertTrue(log_content[len(log_content) - 1].endswith(f'[ DEBUG ] [ {__name__} ]: {__name__}.test_debug:\n'))
|
||||
|
||||
def test_info(self):
|
||||
print(f'{__name__}.test_info:')
|
||||
logger: Logger = self._services.get_service(LoggerBase)
|
||||
logger.create()
|
||||
logger.info(__name__, f'{__name__}.test_info:')
|
||||
|
||||
log_file = Template(self._log_settings.filename).substitute(
|
||||
date_time_now=self._app_host.date_time_now.strftime(self._time_format_settings.date_time_format),
|
||||
start_time=self._app_host.start_time.strftime(self._time_format_settings.date_time_log_format)
|
||||
)
|
||||
log_content = []
|
||||
|
||||
try:
|
||||
with open(self._log_settings.path + log_file, "r") as log:
|
||||
log_content = log.readlines()
|
||||
log.close()
|
||||
except Exception as e:
|
||||
print('Cannot open log file', e)
|
||||
|
||||
self.assertGreater(len(log_content), 0)
|
||||
self.assertTrue(log_content[len(log_content) - 1].endswith(f'[ INFO ] [ {__name__} ]: {__name__}.test_info:\n'))
|
||||
|
||||
def test_warn(self):
|
||||
print(f'{__name__}.test_warn:')
|
||||
logger: Logger = self._services.get_service(LoggerBase)
|
||||
logger.create()
|
||||
logger.warn(__name__, f'{__name__}.test_warn:')
|
||||
|
||||
log_file = Template(self._log_settings.filename).substitute(
|
||||
date_time_now=self._app_host.date_time_now.strftime(self._time_format_settings.date_time_format),
|
||||
start_time=self._app_host.start_time.strftime(self._time_format_settings.date_time_log_format)
|
||||
)
|
||||
log_content = []
|
||||
|
||||
try:
|
||||
with open(self._log_settings.path + log_file, "r") as log:
|
||||
log_content = log.readlines()
|
||||
log.close()
|
||||
except Exception as e:
|
||||
print('Cannot open log file', e)
|
||||
|
||||
self.assertGreater(len(log_content), 0)
|
||||
self.assertTrue(log_content[len(log_content) - 1].endswith(f'[ WARN ] [ {__name__} ]: {__name__}.test_warn:\n'))
|
||||
|
||||
def test_error(self):
|
||||
print(f'{__name__}.test_error:')
|
||||
logger: Logger = self._services.get_service(LoggerBase)
|
||||
logger.create()
|
||||
logger.error(__name__, f'{__name__}.test_error:')
|
||||
|
||||
log_file = Template(self._log_settings.filename).substitute(
|
||||
date_time_now=self._app_host.date_time_now.strftime(self._time_format_settings.date_time_format),
|
||||
start_time=self._app_host.start_time.strftime(self._time_format_settings.date_time_log_format)
|
||||
)
|
||||
log_content = []
|
||||
|
||||
try:
|
||||
with open(self._log_settings.path + log_file, "r") as log:
|
||||
log_content = log.readlines()
|
||||
log.close()
|
||||
except Exception as e:
|
||||
print('Cannot open log file', e)
|
||||
|
||||
self.assertGreater(len(log_content), 0)
|
||||
self.assertTrue(log_content[len(log_content) - 1].endswith(f'[ ERROR ] [ {__name__} ]: {__name__}.test_error:\n'))
|
||||
|
||||
def test_fatal(self):
|
||||
print(f'{__name__}.test_fatal:')
|
||||
logger: Logger = self._services.get_service(LoggerBase)
|
||||
logger.create()
|
||||
with self.assertRaises(SystemExit):
|
||||
logger.fatal(__name__, f'{__name__}.test_fatal:')
|
||||
|
||||
log_file = Template(self._log_settings.filename).substitute(
|
||||
date_time_now=self._app_host.date_time_now.strftime(self._time_format_settings.date_time_format),
|
||||
start_time=self._app_host.start_time.strftime(self._time_format_settings.date_time_log_format)
|
||||
)
|
||||
log_content = []
|
||||
|
||||
try:
|
||||
with open(self._log_settings.path + log_file, "r") as log:
|
||||
log_content = log.readlines()
|
||||
log.close()
|
||||
except Exception as e:
|
||||
print('Cannot open log file', e)
|
||||
|
||||
self.assertGreater(len(log_content), 0)
|
||||
self.assertTrue(log_content[len(log_content) - 1].endswith(f'[ ERROR ] [ {__name__} ]: {__name__}.test_fatal:\n'))
|
||||
|
@ -23,11 +23,18 @@ class Tester:
|
||||
self._suite.addTest(LoggerTest('test_create'))
|
||||
self._suite.addTest(LoggerTest('test_header'))
|
||||
self._suite.addTest(LoggerTest('test_trace'))
|
||||
self._suite.addTest(LoggerTest('test_debug'))
|
||||
self._suite.addTest(LoggerTest('test_info'))
|
||||
self._suite.addTest(LoggerTest('test_warn'))
|
||||
self._suite.addTest(LoggerTest('test_error'))
|
||||
self._suite.addTest(LoggerTest('test_fatal'))
|
||||
|
||||
# publishing
|
||||
|
||||
def start(self):
|
||||
unittest.main()
|
||||
runner = unittest.TextTestRunner()
|
||||
runner.run(self._suite)
|
||||
# unittest.main()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
Loading…
Reference in New Issue
Block a user