From 8c10f71f58fdb9fc6cdabdd63df1a576969562ad Mon Sep 17 00:00:00 2001 From: forest Date: Tue, 27 Jul 2021 14:44:10 -0500 Subject: [PATCH] getting unit tests to log properly --- .gitignore | 2 +- capsulflask/shared.py | 24 +++++++++++++++++------- capsulflask/tests_base.py | 4 ++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 0a82cef..b343e04 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,4 @@ build/ *.egg-info/ .venv -unittest-output.log \ No newline at end of file +unittest-log-output.log \ No newline at end of file diff --git a/capsulflask/shared.py b/capsulflask/shared.py index 92e8485..68af334 100644 --- a/capsulflask/shared.py +++ b/capsulflask/shared.py @@ -56,28 +56,38 @@ def authorized_as_hub(headers): def my_exec_info_message(exec_info): 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): - 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.write(message) + file_object.write(f"{mylog_current_test_id_container['value']}: {message}\n") file_object.close() + 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) 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) 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) 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) 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) \ No newline at end of file diff --git a/capsulflask/tests_base.py b/capsulflask/tests_base.py index 085d739..9a3f93e 100644 --- a/capsulflask/tests_base.py +++ b/capsulflask/tests_base.py @@ -23,10 +23,10 @@ class BaseTestCase(TestCase): return self.app def setUp(self): - mylog_info(self.app, f"setting up {self.id()}") + set_mylog_test_id(self.id()) def tearDown(self): - mylog_info(self.app, f"tearing down {self.id()}") + set_mylog_test_id("") def _login(self, user_email): get_model().login(user_email)