I know how to assert that a log message was generated, but I can't seem to figure out how to assert that a log message was not generated. Here's the unit test I have now (sanitized). Note that XYZ class takes the logger as a param, and test_check_unexpected_keys_found passes as expected.
import unittest
import logging
class TestXYZ(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.test_logger = logging.getLogger('test_logger')
cls.test_logger.addHandler(logging.NullHandler())
def test_check_unexpected_keys_found(self):
test_dict = {
'unexpected': 0,
'expected1': 1,
'expected2': 2,
}
xyz = XYZ(self.test_logger)
with self.assertLogs('test_logger', level='WARNING'):
xyz._check_unexpected_keys(test_dict)
def test_check_unexpected_keys_none(self):
test_dict = {
'expected1': 1,
'expected2': 2,
}
xyz = XYZ(self.test_logger)
xyz._check_unexpected_keys(test_dict)
# assert that 'test_logger' was not called ??
I tried using unittest.patch like so:
with patch('TestXYZ.test_logger.warning') as mock_logwarn:
xyz._check_unexpected_keys(test_dict)
self.assertFalse(mock_logwarn.called)
But I get
ImportError: No module named 'TestXYZ'I tried some variants on that as well, but got nowhere.
Anyone know how to handle this?
XYZ, why not just pass in a mock? Why do you need to patch, or create a real logger?