Skip to content

Commit

Permalink
Update test_monitor_logger.py
Browse files Browse the repository at this point in the history
  • Loading branch information
yihong1120 authored May 8, 2024
1 parent 1113aa3 commit 212997c
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions tests/test_monitor_logger.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from src.monitor_logger import setup_logging
from src.monitor_logger import LoggerConfig
import logging
import os

Expand All @@ -8,34 +8,35 @@ class TestMonitorLogger(unittest.TestCase):
Test cases for monitor_logger.py.
"""

def test_setup_logging(self):
def test_logger_configuration(self):
"""
Test the logging setup to ensure it creates log files and logs messages correctly.
Test the logger configuration to ensure it creates log files and logs messages correctly.
"""
# Call the setup_logging function from monitor_logger.py
logger = setup_logging()
# Initialise the logger configuration using the LoggerConfig class
logger_config = LoggerConfig(log_file='site_safety.log')
logger = logger_config.get_logger()

# Check if the logger is an instance of logging.Logger
self.assertIsInstance(logger, logging.Logger, "The logger should be an instance of logging.Logger")

# Check if the log file is created
log_file = 'site_safety.log'
self.assertTrue(os.path.isfile(log_file), "Log file should exist")
# Check if the log file is created in the logs directory
log_file_path = 'logs/site_safety.log'
self.assertTrue(os.path.isfile(log_file_path), "Log file should exist")

# Check if the logger can log a message
test_message = "This is a test log message."
logger.info(test_message)

# Read the last line from the log file
with open(log_file, 'r') as file:
with open(log_file_path, 'r') as file:
logs = file.readlines()
last_log = logs[-1] if logs else ''

# Check if the last log contains the test message
self.assertIn(test_message, last_log, "The log file should contain the test log message")

# Clean up the log file after the test
os.remove(log_file)
os.remove(log_file_path)

if __name__ == '__main__':
unittest.main()
unittest.main()

0 comments on commit 212997c

Please sign in to comment.