Hi all, While working on a ticket that had to do with cookies, I found myself writing this code (essentially):
with warnings.catch_warnings(record=True) as warnings_log: # # Begin actual code I'm trying to test # CsrfViewMiddleware().process_view(req, token_view, (), {}) # # End actual code I'm trying to test # if (len(warnings_log) > 1 or not issubclass(warnings_log[0].category, UnicodeWarning)): for warning in warnings_log: warnings.warn(warning) The code I'm trying to test is going to make a UnicodeWarning, because the whole point of the test is to see how the code deals with broken user input. Having to wrap it in 5 lines of unrelated code feels odd. So I thought a context manager to suppress specific warnings in a block might be generally useful, and for tests, it may be even useful to have an assertWarns -- an analog of assertRaises. Opinions? Thanks, Shai.