From aa67a1e1b2f8d90f1b224170de8dfa0f20c567c9 Mon Sep 17 00:00:00 2001 From: forest Date: Tue, 27 Jul 2021 13:28:49 -0500 Subject: [PATCH] blah failed attempts at getting tests to log --- capsulflask/tests/test_console.py | 10 +++++++++- capsulflask/tests_base.py | 19 ++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/capsulflask/tests/test_console.py b/capsulflask/tests/test_console.py index 23d200a..fee1178 100644 --- a/capsulflask/tests/test_console.py +++ b/capsulflask/tests/test_console.py @@ -1,3 +1,5 @@ +import logging + from copy import deepcopy from unittest.mock import patch @@ -80,6 +82,10 @@ class ConsoleTests(BaseTestCase): len(get_model().list_vms_for_account('test@example.com')), 0 ) + + file_object = open('unittest-output.log', 'a') + file_object.write(f"{self.id()} captured output:\n{self.logs_from_test.getvalue()}\n") + file_object.close() def test_create_fails_invalid(self): with self.client as client: @@ -129,7 +135,7 @@ class ConsoleTests(BaseTestCase): ) vm_id = vms[0]['id'] - + self.assertRedirects( response, url_for("console.index") + f'?created={vm_id}' @@ -190,10 +196,12 @@ class ConsoleTests(BaseTestCase): ) def setUp(self): + super().setUp() self._login('test@example.com') get_model().create_ssh_public_key('test@example.com', 'key', 'foo') def tearDown(self): + super().tearDown() get_model().cursor.execute("DELETE FROM ssh_public_keys") get_model().cursor.execute("DELETE FROM login_tokens") get_model().cursor.execute("DELETE FROM vms") diff --git a/capsulflask/tests_base.py b/capsulflask/tests_base.py index 43c9ed5..f343f7c 100644 --- a/capsulflask/tests_base.py +++ b/capsulflask/tests_base.py @@ -1,7 +1,11 @@ +from io import StringIO +import logging +import unittest import os from nanoid import generate from flask_testing import TestCase +from flask import current_app from capsulflask import create_app from capsulflask.db import get_model @@ -14,16 +18,25 @@ class BaseTestCase(TestCase): os.environ['LOG_LEVEL'] = 'DEBUG' os.environ['SPOKE_MODEL'] = 'mock' os.environ['HUB_MODEL'] = 'capsul-flask' - return create_app() + self.app = create_app() + return self.app def setUp(self): - pass + self.logs_from_test = StringIO() + self.logs_handler = logging.StreamHandler(self.logs_from_test) + current_app.logger.addHandler(self.logs_handler) def tearDown(self): - pass + #self.app.logger.removeHandler(self.logs_handler) + #logging.getLogger("asdasd1").warning(f"{self.id()} captured output:\n{self.logs_from_test.getvalue()}\n") + + file_object = open('unittest-output.log', 'a') + file_object.write(f"{self.id()} captured output:\n{self.logs_from_test.getvalue()}\n") + file_object.close() def _login(self, user_email): get_model().login(user_email) with self.client.session_transaction() as session: session['account'] = user_email session['csrf-token'] = generate() +