-
Notifications
You must be signed in to change notification settings - Fork 16
Make assertions to show a custom message when raising/warning #208
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
Comments
Would adding a "msg" argument to all assert* functions be enough, or do you want to include the values of both sides? In the later case, I guess we could handle that in *args) I wonder if this would work: |
Something like
would be very useful since it would be flexible enough to dump some information à la show ! |
I renew my interest in this feature. Something like
which would behave like
when the assertion fails |
Initially I thought I would rather have the signature be: assertEqual(first, second, msg=None) where msg is either None or a string. i.e., let users build the message string themselves. But this cannot work because when users type something like: assertEqual(first, second, '%s abc %s' % (first, second)) the string is evaluated immediately (at the string "definition"), and so our expression evaluator only ever sees: assertEqual(a, b, a, "is super different from", b)
# OR
assertEqual(a, b, (a, "is super different from", b)) I am ambivalent to either. As for the implementation, it should not be too hard to implement. The code is in actions.py. Here is a proof of concept of the later for ComparisonAssert: def eval_assertion(self, context, v1, v2, msg=None):
result = self.compare(v1, v2)
if isinstance(result, tuple):
result, details = result
else:
details = ''
if not result:
op = self.inv_op
if isinstance(msg, tuple):
msg = ' '.join(str(v) for v in msg)
msg = ': {}'.format(msg) if msg is not None else ''
return "%s %s %s (%s %s %s)%s%s" % (self.args[0], op, self.args[1],
v1, op, v2, details, msg) If you could generalize this to all assertion functions, modify the corresponding documentation, add a note in the changelog and make a PR out of this, that would be nice :) |
I will definitively try to implement it along the lines you described.
- assertion: 1 == 0
- while(not(assertion):
- show(dump(diagnostics)) |
I think the above proof-of-concept should work. For the user, that would look like: assertEqual(1, 0, msg=dump(diagnostics)) |
also documented assertRaises and added tests for assert functions
The message would be similar to the ones available through the show command.
The text was updated successfully, but these errors were encountered: