def test_fileLoggerAppendFileTest(self): file_logger = FileLogger("", True, MessageType.INFORMATION, "WriteToExistingFileLogger") file_logger.log_message("This is a test to write to an existing file.", None, MessageType.WARNING) # os.remove(file_logger.get_file_path()) self.assertTrue(path.exists(file_logger.get_file_path()))
def test_HierarchicalTxtFileLogger(self): log_level, levels = self.single_data file_logger = FileLogger( LoggingConfig().get_log_directory(), True, self.get_file_name(self._testMethodName + log_level, "txt"), MessageType.GENERIC) self.hierarchical_logging(file_logger, file_logger.get_file_path(), log_level, levels) os.remove(file_logger.get_file_path()) self.assertTrue(path.exists(file_logger.get_file_path()))
def test_FileLoggerCatchThrownException(self): file_logger = FileLogger("", True, "FileLoggerCatchThrownException", MessageType.GENERIC) file_logger.set_file_path("<>") file_logger.log_message(MessageType.GENERIC, "test throws error") os.remove(file_logger.get_file_path()) self.assertTrue(path.exists(file_logger.get_file_path()))
def test_SoftAssertIsFalseTest(): soft_assert = SoftAssert( FileLogger(LoggingConfig.get_log_directory(), "UnitTests.SoftAssertUnitTests.SoftAssertIsFalseTest", MessageType.GENERIC.value, True)) soft_assert.assertFalse(2 == 1, "Test") soft_assert.fail_test_if_assert_failed()
def test_WriteToExistingFileLogger(self): file_logger = FileLogger("", True, "WriteToExistingFileLogger", MessageType.GENERIC) file_logger.log_message("This is a test.", None, MessageType.WARNING) file_logger.log_message( MessageType.WARNING, "This is a test to write to an existing file.") os.remove(file_logger.get_file_path()) self.assertTrue(path.exists(file_logger.get_file_path()))
def test_SoftAssertValidTest(): soft_assert = SoftAssert( FileLogger(LoggingConfig.get_log_directory(), "UnitTests.SoftAssertUnitTests.SoftAssertValidTest")) soft_assert.assertEquals("Yes", "Yes", "Utilities Soft Assert", "Message is not equal") soft_assert.assertEquals("YesAgain", "YesAgain", "Utilities Soft Assert 2") soft_assert.fail_test_if_assert_failed()
def test_SoftAssertFailTest(self): with self.assertRaises(AttributeError): soft_assert = SoftAssert( FileLogger(LoggingConfig.get_log_directory(), "UnitTests.SoftAssertUnitTests.SoftAssertFailTest")) soft_assert.assertEquals("Yes", "No", "Utilities Soft Assert", "Message is not equal") soft_assert.assertEquals("Yes", "NoAgain", "Utilities Soft Assert 2") soft_assert.fail_test_if_assert_failed()
def test_SoftAssertIsTrueTestFailure(self): with self.assertRaises(AttributeError): soft_assert = SoftAssert( FileLogger(LoggingConfig.get_log_directory(), "UnitTests.SoftAssertUnitTests.SoftAssertFailTest", MessageType.GENERIC.name, True)) soft_assert.assertTrue(1 == 2, "Test") soft_assert.assertTrue(1 == 2, "Test1") soft_assert.assertTrue(True, "Test2") soft_assert.fail_test_if_assert_failed()
def test_SoftAssertDidFailCheck(self): soft_assert = SoftAssert( FileLogger(LoggingConfig.get_log_directory(), "UnitTests.SoftAssertUnitTests.SoftAssertIsTrueTest", MessageType.GENERIC.name, True)) soft_assert.assertTrue(True, "Test1") self.assertFalse(soft_assert.did_soft_asserts_fail()) soft_assert.assertTrue(1 == 2, "Test2") self.assertTrue(soft_assert.did_soft_asserts_fail())
def test_FileLoggerLogMessage(self): file_logger = FileLogger("", True, "FileLoggerLogMessage") file_logger.log_message("Test to ensure LogMessage works as expected.") self.assertTrue( self.Test_Message in self.read_text_file(file_logger.get_file_path()), "Expected Log Message to be contained in log.") os.remove(file_logger.get_file_path()) self.assertTrue(path.exists(file_logger.get_file_path()))
def test_FileLoggerNoParameters(self): file_logger = FileLogger("") self.assertEquals( tempfile.TemporaryFile(), file_logger.get_directory(), StringProcessor.safe_formatter("Expected Directory '{}'.", file_logger.DEFAULT_LOG_FOLDER)) self.assertEquals("FileLog.txt", file_logger.get_file_name(), "Expected correct File Name.") self.assertEquals(MessageType.INFORMATION, file_logger.get_message_type(), "Expected Information Message Type.") os.remove(file_logger.get_file_path()) self.assertTrue(path.exists(file_logger.get_file_path()))
def test_SoftAssertVerifyCheckForFailures(self): soft_assert = SoftAssert( FileLogger( LoggingConfig.get_log_directory(), "UnitTests.SoftAssertUnitTests.SoftAssertVerifyCheckForFailures" )) soft_assert.assertEquals("Yes", "Yes", "Utilities Soft Assert", "Message is not equal") soft_assert.fail_test_if_assert_failed() self.assertTrue(soft_assert.did_user_check()) soft_assert.assertEquals("Yes", "Yes", "Utilities Soft Assert", "Message is not equal") self.assertFalse(soft_assert.did_user_check())
def test_FileLoggerAppendMessagingLevel(self): file_logger = FileLogger(True, MessageType.WARNING.value) self.assertEquals( tempfile.TemporaryFile(), file_logger.get_directory(), StringProcessor.safe_formatter("Expected Directory '{}'.", file_logger.DEFAULT_LOG_FOLDER)) self.assertEquals("FileLog.txt", file_logger.get_file_name(), "Expected correct File Name.") self.assertEquals(MessageType.WARNING.value, file_logger.get_message_type(), "Expected Warning Message Type.") os.remove(file_logger.get_file_path()) self.assertTrue(path.exists(file_logger.get_file_path()))
def test_FileLoggerAppendLogFolder(self): append_file_directory = LoggingConfig().get_log_directory( ) + "/" + "Append File Directory" file_logger = FileLogger(append_file_directory, True) self.assertEquals(append_file_directory, file_logger.get_directory(), "Expected Directory 'Append File Directory'.") self.assertEquals("FileLog.txt", file_logger.get_file_name(), "Expected correct File Name.") self.assertEquals(MessageType.INFORMATION.name, file_logger.get_message_type(), "Expected Information Message Type.") os.remove(file_logger.get_file_path()) self.assertTrue(path.exists(file_logger.get_file_path()))
def test_FileLoggerLogFolderMessagingLevel(self): file_logger = FileLogger(self.LOG_FOLDER_MESSAGING_LEVEL_DIRECTORY, MessageType.WARNING) self.assertEquals( self.LOG_FOLDER_MESSAGING_LEVEL_DIRECTORY, file_logger.get_directory(), "Expected Directory 'Log Folder Messaging Level Directory'.") self.assertEquals("FileLog.txt", file_logger.get_file_name(), "Expected correct File Name.") self.assertEquals(MessageType.WARNING.value, file_logger.get_message_type(), "Expected Warning Message Type.") os.remove(file_logger.get_file_path()) self.assertTrue(path.exists(file_logger.get_file_path()))
def test_FileLoggerLogFolderFileName(self): log_folder_file_name_directory = LoggingConfig().get_log_directory( ) + "/" + "Log Folder File Name Directory" file_logger = FileLogger(log_folder_file_name_directory, "LogFolderFileName.txt") self.assertEquals( log_folder_file_name_directory, file_logger.get_directory(), "Expected Directory 'Log Folder File Name Directory'.") self.assertEquals("LogFolderFileName.txt", file_logger.get_file_name(), "Expected correct File Name.") self.assertEquals(MessageType.INFORMATION, file_logger.get_message_type(), "Expected Information Message Type.") os.remove(file_logger.get_file_path()) self.assertTrue(path.exists(file_logger.get_file_path()))
def test_FileLoggerAppendLogFolderMessagingLevel(self): append_log_folder_file_name_directory = LoggingConfig().get_log_directory() + "/" + \ "AppendLogFolderFileNameDirectory " file_logger = FileLogger(True, append_log_folder_file_name_directory, MessageType.WARNING) self.assertEquals( append_log_folder_file_name_directory, file_logger.get_directory(), " Expected Directory AppendLogFolderFileNameDirectory") self.assertEquals("FileLog.txt", file_logger.get_file_name(), "Expected correct File Name.") self.assertEquals(MessageType.WARNING.name, file_logger.get_message_type(), "Expected Warning Message Type.") os.remove(file_logger.get_file_path()) self.assertTrue(path.exists(file_logger.get_file_path()))
def test_SoftAssertFailsValidTest(self): soft_assert = SoftAssert( FileLogger( LoggingConfig.get_log_directory(), "UnitTests.SoftAssertUnitTests.SoftAssertFailsValidTest")) soft_assert.fails( lambda x: self.MethodThrowsNoneException(), TypeError, "Assert Method Throws Explicit Exception", "Failed to assert that method threw a NullReferenceException") result = 9 / 0 soft_assert.fails( self.fail( f"Result should have thrown an error but is {result} instead"), # soft_assert.assertFails(lambda x: (result = 9 / 0) self.fail(f"Result should have thrown # an error but is {result} instead"), ZeroDivisionError, "Assert action throws divide by zero exception", "Failed to assert that we couldn't divide by zero") soft_assert.fail_test_if_assert_failed()
def get_logger(self, file_name): # Disable logging means we just send any logged messages to the console if self.get_logging_enabled_setting( self.config) == LoggingEnabled.NO.name: return ConsoleLogger() log_directory = self.get_log_directory() logging_level = self.get_logging_level_setting(Config()) # value = Config().get_general_value("LogType", "CONSOLE").upper() value = self.config.get_general_value("LogType").upper() if value == "CONSOLE": return ConsoleLogger(logging_level) elif value == "TXT": return FileLogger(log_directory, False, file_name, logging_level) else: raise AttributeError( StringProcessor.safe_formatter( "Log type {} is not a valid option", Config().get_general_value("LogType", "CONSOLE")))
def test_FileLoggerAppendOnly(self): file_logger = FileLogger("", True) self.assertEquals( file_logger.DEFAULT_LOG_FOLDER, file_logger.get_directory(), StringProcessor.safe_formatter("Expected Directory '{}'.", file_logger.DEFAULT_LOG_FOLDER)) self.assertEquals("FileLog.txt", file_logger.get_file_name(), "Expected correct File Name.") self.assertEquals(MessageType.INFORMATION.name, file_logger.get_message_type(), "Expected Information Message Type.") # os.remove(file_logger.get_file_path() + file_logger.get_file_name()) self.assertFalse( path.exists(os.path.abspath(file_logger.get_file_path())))
def test_FileLoggerLogFolderFileNameMessagingLevel(self): log_folder_file_name_messaging_level_directory = LoggingConfig().get_log_directory() + "/" + \ "LogFolderFileNameMessagingLevelDirectory" file_logger = FileLogger( log_folder_file_name_messaging_level_directory, "LogFolderFileNameMessagingLevel.txt", MessageType.WARNING) self.assertEquals( log_folder_file_name_messaging_level_directory, file_logger.get_directory(), "Expected Directory 'LogFolderFileNameMessagingLevelDirectory'") self.assertEquals("LogFolderFileNameMessagingLevel.txt", file_logger.get_file_name(), "Expected correct File Name.") self.assertEquals(MessageType.WARNING.name, file_logger.get_message_type(), "Expected Warning Message Type.") os.remove(file_logger.get_file_path()) self.assertTrue(path.exists(file_logger.get_file_path()))
def test_FileLoggerConstructorCreateDirectory(self): message = "Test to ensure that the file in the created directory can be written to." file_logger = FileLogger(LoggingConfig().get_log_directory(), True, "FileLoggerCreateDirectoryDelete", MessageType.GENERIC) file_logger.log_message( MessageType.WARNING.value, "Test to ensure that the file in the created directory can be written to." ) file = file_logger.get_file_path() actual_message = self.read_text_file(file.getCanonicalPath()) self.assertTrue( message in actual_message, "Expected '" + message + "' but got '" + actual_message + "' for: " + file.getCanonicalPath()) os.remove(file) self.assertTrue(path.exists(file_logger.get_file_path()))
def test_SoftAssertFailsInvalidTest(self): with self.assertRaises(AttributeError): soft_assert = SoftAssert( FileLogger( LoggingConfig.get_log_directory(), "UnitTests.SoftAssertUnitTests.SoftAssertFailsInvalidTest") ) soft_assert.fails( lambda x: self.MethodThrowsNoneException(), NotImplementedError, "Assert Method Throws Explicit Exception", "Failed to assert that method threw a NotImplementedException") result = 9 / 0 soft_assert.fails( self.fail( f"Result should have thrown an error but is {result} instead" ), # soft_assert.assertFails(lambda x: (result = 9 / 0) self.fail(f"Result should have # thrown an error but is {result} instead"), TypeError, "Assert dividing by zero throws a null reference", "Failed to assert that we couldn't divide by zero") soft_assert.fail_test_if_assert_failed()
def Test_SuspendLogger(self): # Start logging file_logger = FileLogger( True, LoggingConfig().get_log_directory(), self.get_file_name("TestHierarchicalTxtFileLogger", "txt"), MessageType.GENERIC.value) file_logger.set_logging_level(MessageType.VERBOSE) file_logger.log_message(MessageType.VERBOSE, "HellO") # Suspend logging file_logger.suspend_logging() file_logger.log_message(MessageType.ERROR, "GoodByE") # Continue logging file_logger.continue_logging() file_logger.log_message(MessageType.VERBOSE, "BacK") # Get the log file content log_contents = self.read_text_file(file_logger.get_file_path()) # Verify that logging was active hello_found = "HellO" in log_contents self.assertTrue(hello_found, "'HellO' was not found. Logging Failed") # Verify that logging was suspended goodbye_found = "GoodByE" in log_contents self.assertFalse( goodbye_found, "'GoodByE' was found when it should not be written. Logging Failed" ) # Verify that logging was active back_found = "BacK" in log_contents self.assertTrue(back_found, "'BacK' was not found. Logging Failed") os.remove(file_logger.get_file_path()) self.assertTrue(path.exists(file_logger.get_file_path()))
def fileLoggerTest(self): self.set_logger(FileLogger()) if not isinstance(FileLogger, self.get_logger()): self.fail("FileLogger was not set.")
def test_FileLoggerSetFilePath(self): file_logger = FileLogger("", True, "FileLoggerSetFilePath", MessageType.GENERIC) file_logger.set_file_path("test file path") self.assertEquals(file_logger.get_file_path(), "test file path") self.assertFalse(path.exists(file_logger.get_file_path()))
def test_SoftAssertVerifyUserCheck(self): soft_assert = SoftAssert( FileLogger( LoggingConfig.get_log_directory(), "UnitTests.SoftAssertUnitTests.SoftAssertVerifyUserCheck")) self.assertTrue(soft_assert.did_user_check())
def test_FileLoggerEmptyFileNameException(self): with self.assertRaises(AttributeError): logger = FileLogger("") self.assertTrue(logger is None)
def test_WriteToFileLogger(self): file_logger = FileLogger("", False, "WriteToFileLogger") file_logger.log_message(MessageType.WARNING.value, "Hello, this is a test.") os.remove(file_logger.get_file_path()) self.assertTrue(path.exists(file_logger.get_file_path()))