-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add unittest assertion for logging #63137
Comments
It is sometimes useful to check for the log messages emitted by some piece of code (especially a library). Would it be a good idea to add a dedicated assertion method for that? I would propose the following API: with self.assertLogging("logger.name", level="WARN") as cm:
... The |
I have project in which that would be useful, so +1 from me for the general concept. |
I agree a context manager would be useful. Note that I have already provided a Handler subclass (TestHandler) as well as a Matcher class which allows matching of LogRecords, and which can be used in assertions. These are in test.support as they were originally intended for Python's own tests, but could be moved to the logging package if they are considered more generally useful. See this for how to use them: http://plumberjack.blogspot.co.uk/2010/09/unit-testing-and-logging.html |
Whoops, meant to say "... moved to the unittest package ...". |
Proposed patch with an assertLogs() method. |
So making assertions about logging is very common, but I *always* do this by just mocking out the logger and making an assertion about the call. It's trivially easy to do. |
Only works if you test the exact logger that will be triggered, rather |
Do you have a lot of circumstances where you want to test logging but you don't know the specific logger and method called? That's not a situation I've been in. |
I have a lot of circumstances where I do not *care* about the specific |
Exactly. It is the difference between black box testing and white box testing. IMO mock, while a great tool, should be used with caution, because it tends to lead to white box testing. |
The patch and approach look good to me (with docs needed). Note, personally I would not use a leading underscore for the assertion methods (in the tests) _assertNoStderr and _assertLogRecords. |
Question: should the default level threshold be INFO or WARNING? |
Here is an updated patch:
|
David told me he was in favour of keeping INFO the default, so I'll leave it like that. |
New changeset 4f5815747f58 by Antoine Pitrou in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: