nkanderson / ECE585-llc

Simulation of a last level cache (LLC) for ECE 585 final project
1 stars 0 forks source link

Add mocking for project config and logger in `test_cache.py` #48

Closed nkanderson closed 15 hours ago

nkanderson commented 2 days ago

We should mock the logger and args in test_cache.py, like in this setUp and tearDown from the event_handler. Relevant lines below:

def setUp(self):
    # Mocking the config logger and args for testing
    self.mock_logger = MagicMock()
    self.mock_args = MagicMock(silent=False, debug=True)

    # Patch the config logger and args
    patch(
        "config.project_config.config.get_logger", return_value=self.mock_logger
    ).start()
    patch(
        "config.project_config.config.get_args", return_value=self.mock_args
    ).start()

def tearDown(self):
    # Stop any patches done during the test
    patch.stopall()

Mocking the logger means we can test whether it was called without manually verifying the test output, for example:

# Verify logger called with correct message for opcode 1
self.mock_logger.log.assert_called_once_with(
    LogLevel.DEBUG, "write request from L1 data cache"
)

Until we're getting into functional tests for the program as a whole, it's better to prevent any attempts by the sections of the program we're running from parsing args.

We could mock cache_config as well, or add separate tests passing a cache_config instance directly to the cache init method, which would provide flexibility in the test to try different values, including invalid config options.

nkanderson commented 15 hours ago

Closed by #50