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

Make assertions to show a custom message when raising/warning #208

Closed
benjello opened this Issue Feb 3, 2016 · 6 comments

Comments

Projects
None yet
2 participants
@benjello
Contributor

benjello commented Feb 3, 2016

The message would be similar to the ones available through the show command.

@gdementen

This comment has been minimized.

Member

gdementen commented Feb 3, 2016

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)
eg. assertEqual(a, b, a, "is super different from", b)

I wonder if this would work:
assertEqual(a, b, "%s is super different from %s" % (a, b))

@benjello

This comment has been minimized.

Contributor

benjello commented Feb 3, 2016

Something like

assertEqual(a, b, a, "is super different from", b)

would be very useful since it would be flexible enough to dump some information à la show !

@benjello

This comment has been minimized.

Contributor

benjello commented Apr 28, 2017

I renew my interest in this feature. Something like

assertTrue(expression, message = show_args_tuple)

which would behave like

show(*show_args_tuple)

when the assertion fails

@gdementen

This comment has been minimized.

Member

gdementen commented May 2, 2017

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(first, second, 'first abc second') which we cannot do much with. That leaves us with either:

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 :)

@benjello

This comment has been minimized.

Contributor

benjello commented May 2, 2017

I will definitively try to implement it along the lines you described.
But I would also like to see it would be possible to dump some diagnostics when the assertion fails:

  • Is it possible to evaluate an expression using the same context within eval_assertion ?
  • Should I use liam2 control statements to catch failed assertion and dump diagnostics ?
 - assertion: 1 == 0
 - while(not(assertion):
    - show(dump(diagnostics))
@gdementen

This comment has been minimized.

Member

gdementen commented May 2, 2017

I think the above proof-of-concept should work. For the user, that would look like:

assertEqual(1, 0, msg=dump(diagnostics))

@gdementen gdementen added this to the 0.12 milestone Jan 19, 2018

@gdementen gdementen closed this in 2276a81 Jan 23, 2018

gdementen added a commit that referenced this issue Jan 25, 2018

implemented msg in all assert functions (closes #208)
also documented assertRaises and added tests for assert functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment