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.
We should mock the logger and args in
test_cache.py
, like in thissetUp
andtearDown
from the event_handler. Relevant lines below:Mocking the logger means we can test whether it was called without manually verifying the test output, for example:
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 acache_config
instance directly to the cache init method, which would provide flexibility in the test to try different values, including invalid config options.