unittest assertRises question

Tim Peters tim.one at comcast.net
Fri Dec 27 17:02:14 EST 2002


[Leazen]
>   I'm using unittest and have a problem when trying to raise an
> exception when creating an object.
>   What I understood from the traceback is that I should be giving one
> more parameter in the last argument of assertRaises. Is it asking for
> self?

Nope.

> I don't get it. Could anyone please explain what's going on?

Yup <wink>.

> The following bit of code:
>
>     def testPriorities(self):
>         """Priorities should be [0,2]."""
>         for i in range(-10, 0) + range(3, 10):
>             self.assertRaises(log.InvalidPriority, log.LogEvent, (i,
> self.msg))

That passes one argument to log.logEvent, the 2-tuple (i, self.msg).  But
LogEvent requires at least two arguments (besides self):

> ...
> class LogEvent:
>     def __init__(self, priority, msg, eventTime = None):

You want to rewrite the call, like so:

    self.assertRaises(log.InvalidPriority, log.LogEvent, i, self.msg)

Either that, or (highly unlikely that you want this) change __init__ like
so:

class LogEvent:
    def __init__(self, (priority, msg), eventTime = None):





More information about the Python-list mailing list