Python: Using logging info in nose/unittest? -


i have test function manipulates internal state of object. object logs following using logging.info().

info:root:_change: test light red info:root:_change: test light green info:root:_change: test light yellow 

how can incorporate nose or unittest function can have test similar this?

def test_thing():     expected_log_output = "info:root:_change: test light red\n" +\                           "info:root:_change: test light green\n" +\                           "info:root:_change: test light yellow\n"      run_thing()     assert actual_log_output matches expected_log_output 

when comes testing logging, mock out logger , ensure called appropriate params. typically this:

class testbackupinstantiation(testcase):     @patch('core.backup.log')     def test_exception_raised_when_instantiating_class(self, m_log):         self.assertraises(ioerror) exc:             backup(afakefactory())         assert_equal(m_log.error.call_count, 1)         assert_that(exc.exception, is_(ioerror)) 

so can make call can test ensure logger called validate message.

i believe can like:

m_log.error.assert_called_with("foo")

i might add, when comes kind of testing love using test frameworks flexmock , mock

also, when comes validating matchers, py-hamcrest awesome.


Comments