Opened 8 years ago

Last modified 7 years ago

#6677 enhancement new

Add utility functionality to trial to make asserts about logging

Reported by: Itamar Turner-Trauring Owned by:
Priority: normal Milestone:
Component: trial Keywords:
Cc: Jonathan Lange Branch:


Trial already has an observer registered; we should use it to allow asserting about logged messages, not just about logged errors.

Change History (7)

comment:1 Changed 8 years ago by DefaultCC Plugin

Cc: Jonathan Lange added

comment:2 Changed 8 years ago by Richard Wall

(In [39701]) a custom assert function for logged messages. refs: #6677

comment:3 Changed 8 years ago by Julian Berman is relevant and appears like it will land in the stdlib.

Would be nice to provide some forwards compatibility.

comment:4 Changed 8 years ago by Jean-Paul Calderone

Thanks for pointing out that ticket, Julian, I wonder what kind of compatibility you have in mind. twisted.python.log is quite different from logging. The best compatibility decision that occurs to me is that we should be absolutely certain not to use the same names as the stdlib is going to use so that we don't have a big mess when someone wants to make assertions about logging while using twisted.trial.unittest.

comment:5 Changed 8 years ago by Julian Berman

Yes, definitely, sorry, I had #6173 open also and had some wires crossed.

Although maybe considering a context-manager API would be nice anyhow (and a different name than assertLogMessage? assertLogs was kinda the most obvious choice there unfortunately).

comment:6 Changed 7 years ago by hodgestar

Patch in

I opted to implement just a very straightforward flushLoggedEvents. Assertion helpers can be implemented on top of that if needed.

Lodash and similar Javascript librarys have nice utilities for picking keys out of dictionaries which would make writing tests fairly clean:

def test_my_logging(self):
    logs = self.flushLoggedEvents()
    self.assertEqual(pick(logs, ['message', 'log_level']), [
        {'message': ("Hi!",), 'log_level': log.INFO},
        {'message': ("Eep!",), 'log_level': log.CRITICAL},

comment:7 Changed 7 years ago by hodgestar

Would appreciate some feedback on the approach.

Note: See TracTickets for help on using tickets.