Added testing for service provider with unittests. Also fixed small bugs, found while testing.

This commit is contained in:
2020-11-23 22:22:17 +01:00
parent dd65bbb182
commit c7dff4a212
10 changed files with 178 additions and 100 deletions

View File

@@ -1,79 +1,28 @@
import os
import sys
import traceback
import unittest
from termcolor import colored
from sh_edraft.configuration import ApplicationHost
from tests.logger import LoggerTest
from tests.publisher import PublisherTest
from tests.service_provider import ServiceProviderTest
from tests.service_provider.service_provider_create import ServiceProviderCreate
from tests.service_provider.service_provider_services import ServiceProviderServices
class Tester:
def __init__(self):
self._app_host = ApplicationHost()
self._suite = unittest.TestSuite()
self._error: bool = False
@staticmethod
def disable_print():
sys.stdout = open(os.devnull, 'w')
@staticmethod
def enable_print():
sys.stdout = sys.__stdout__
def success(self, message: str):
self.enable_print()
print(colored(message, 'green'))
self.disable_print()
def failed(self, message: str):
self.enable_print()
print(colored(message, 'red'))
self.disable_print()
def exception(self):
self.enable_print()
print(colored(traceback.format_exc(), 'red'))
self.disable_print()
def create(self): pass
def create(self):
self._suite.addTest(ServiceProviderCreate('test_create'))
self._suite.addTest(ServiceProviderServices('test_add_singleton'))
self._suite.addTest(ServiceProviderServices('test_get_singleton'))
self._suite.addTest(ServiceProviderServices('test_add_scoped'))
self._suite.addTest(ServiceProviderServices('test_get_scoped'))
self._suite.addTest(ServiceProviderServices('test_add_transient'))
self._suite.addTest(ServiceProviderServices('test_get_transient'))
def start(self):
if not self._error:
try:
LoggerTest.start(self._app_host)
self.success(f'{LoggerTest.__name__} test succeeded.')
except Exception as e:
self._error = True
self.failed(f'{LoggerTest.__name__} test failed!\n{e}')
self.exception()
if not self._error:
try:
ServiceProviderTest.start(self._app_host.services)
self.success(f'{ServiceProviderTest.__name__} test succeeded.')
except Exception as e:
self._error = True
self.failed(f'{ServiceProviderTest.__name__} test failed!\n{e}')
self.exception()
if not self._error:
try:
PublisherTest.start(self._app_host.services)
self.success(f'{PublisherTest.__name__} test succeeded.')
except Exception as e:
self._error = True
self.failed(f'{PublisherTest.__name__} test failed!\n{e}')
self.exception()
unittest.main()
if __name__ == '__main__':
tester = Tester()
tester.create()
tester.disable_print()
tester.start()
tester.enable_print()