-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
caplog: add convenience caplog.message() method? #4223
Comments
Doesn't |
Err, yes, it does. Didn't think of that for some reason! 😆 Nevermind me then! |
One scenario where this doesn't work is when you don't know the full message string - as an example: assert len(caplog.records) == 1
message = caplog.messages[0]
assert message.startswith('OSError while reading PDF.js file:') but probably isn't worth to introduce another helper method for that. Kinda wonder whether I should have some kind of helper class using assert caplog.messages == [helpers.glob('OSError while reading PDF.js file: *')] |
My first thought was to generalize your helper into a class -- say, a There are implementation details in |
Can we reopen this as I think we do not have yet a solution for checking that "one of the log messages starts with a prefix", something that is very common, especially with tools that append errors received from other systems to the logging line. At this moment, the only way I see is to loop among each message and manually match it, that is a lot of code for a simple test. Practical example, checking for: |
Reopening as requested. 👍 @ssbarnea what API exactly do you have in mind? Personally I don't mind adding small helpers like this if they are self-contained and easy to maintain, even if later we grow a more general mechanism as mentioned in #4223 (comment): the simple helper is trivial to add and maintain, while the more general mechanism usually involves tons of discussion and might take years. And once the general mechanism is in-place, we can use it in the implementation of the small utility. |
Thanks for reopening. I am not sure yet but I can give an example on how we ended implementing the check at https://github.com/ansible/ansible-lint/blob/8673e5b27a55b88e0752338403045e9ac1cb2ebd/test/TestUtils.py#L161-L188 I think it would be reasonable to assume that during a test the user may want to assure that a list of log messages are created, some of them being only prefixes. If we can find an easier way to implement it, the better. Maybe matching with regex? Even if we decide that a helper would be too much, it would be great to update the documentation of caplog to include an example covering this. |
After adjusting my test codebase for #3579 I noticed I do this a lot:
With a quick GitHub search it looks like I'm not the only one.
What about adding a
caplog.message()
convenience method which does thelen
assertion and returns the only message?cc @wcooley
The text was updated successfully, but these errors were encountered: