getting unit tests to log properly

This commit is contained in:
forest 2021-07-27 14:44:10 -05:00 committed by 3wc
parent 66dee4d87a
commit 8c10f71f58
3 changed files with 20 additions and 10 deletions

2
.gitignore vendored
View File

@ -17,4 +17,4 @@ build/
*.egg-info/ *.egg-info/
.venv .venv
unittest-output.log unittest-log-output.log

View File

@ -56,28 +56,38 @@ def authorized_as_hub(headers):
def my_exec_info_message(exec_info): def my_exec_info_message(exec_info):
return "{}: {}".format(".".join([exec_info[0].__module__, exec_info[0].__name__]), exec_info[1]) return "{}: {}".format(".".join([exec_info[0].__module__, exec_info[0].__name__]), exec_info[1])
mylog_current_test_id_container = {
'value': '',
}
def set_mylog_test_id(test_id):
mylog_current_test_id_container['value'] = ".".join(test_id.split(".")[-2:])
def log_output_for_tests(app: Flask, message: str): def log_output_for_tests(app: Flask, message: str):
if app.config['TESTING'] != False: if app.config['TESTING'] and mylog_current_test_id_container['value'] != "":
file_object = open('unittest-log-output.log', 'a') file_object = open('unittest-log-output.log', 'a')
file_object.write(message) file_object.write(f"{mylog_current_test_id_container['value']}: {message}\n")
file_object.close() file_object.close()
def mylog_debug(app: Flask, message: str): def mylog_debug(app: Flask, message: str):
log_output_for_tests(app, f"DEBUG: {message}\n") log_output_for_tests(app, f"DEBUG: {message}")
app.logger.debug(message) app.logger.debug(message)
def mylog_info(app: Flask, message: str): def mylog_info(app: Flask, message: str):
log_output_for_tests(app, f"INFO: {message}\n") log_output_for_tests(app, f"INFO: {message}")
app.logger.info(message) app.logger.info(message)
def mylog_warning(app: Flask, message: str): def mylog_warning(app: Flask, message: str):
log_output_for_tests(app, f"WARNING: {message}\n") log_output_for_tests(app, f"WARNING: {message}")
app.logger.warning(message) app.logger.warning(message)
def mylog_error(app: Flask, message: str): def mylog_error(app: Flask, message: str):
log_output_for_tests(app, f"ERROR: {message}\n") log_output_for_tests(app, f"ERROR: {message}")
app.logger.error(message) app.logger.error(message)
def mylog_critical(app: Flask, message: str): def mylog_critical(app: Flask, message: str):
log_output_for_tests(app, f"CRITICAL: {message}\n") log_output_for_tests(app, f"CRITICAL: {message}")
app.logger.critical(message) app.logger.critical(message)

View File

@ -23,10 +23,10 @@ class BaseTestCase(TestCase):
return self.app return self.app
def setUp(self): def setUp(self):
mylog_info(self.app, f"setting up {self.id()}") set_mylog_test_id(self.id())
def tearDown(self): def tearDown(self):
mylog_info(self.app, f"tearing down {self.id()}") set_mylog_test_id("")
def _login(self, user_email): def _login(self, user_email):
get_model().login(user_email) get_model().login(user_email)