unittest issues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • paul koelle

    #1

    unittest issues

    Hi,

    I try to use the "failUnlessRais es()" method from unittest.TestCa se as
    follows:
    [color=blue][color=green]
    >>self.failUnle ssRaises(Integr ityError, myObject.new(** stuff))[/color][/color]

    where IntegrityError is an Exception somewhere deep inside pysqlite.
    Running it spits out:
    [color=blue][color=green]
    >>NameError: global name 'IntegrityError ' is not defined.[/color][/color]

    Do I really need to tell unittest about each and every possible
    Exception it might catch. Whats going on here?

    python 2.2.3 on linux 2.6.7

    thanks
    Paul
  • Alex Martelli

    #2
    Re: unittest issues

    paul koelle <paul@subsignal .org> wrote:
    [color=blue]
    > Hi,
    >
    > I try to use the "failUnlessRais es()" method from unittest.TestCa se as
    > follows:
    >[color=green][color=darkred]
    > >>self.failUnle ssRaises(Integr ityError, myObject.new(** stuff))[/color][/color]
    >
    > where IntegrityError is an Exception somewhere deep inside pysqlite.
    > Running it spits out:
    >[color=green][color=darkred]
    > >>NameError: global name 'IntegrityError ' is not defined.[/color][/color]
    >
    > Do I really need to tell unittest about each and every possible
    > Exception it might catch. Whats going on here?[/color]

    You need to define any name you use before you use it, quite apart from
    unittest. You can't use a name that just isn't defined in that scope,
    period: whether you're unit testing or doing anything else, you can't.

    Once you have fixed this you'll find another problem: you need to pass
    failUnlessRaise s the callable and arguments separately, so it can
    perform the call within a try clause. What you're doing here is calling
    the myObject.new method yourself, so it presumably raises well before
    any kind of control is passed to the failUnlessRaise s method -- no good.
    I'm not sure you can pass an arbitrary set of keywords that way (at the
    very least some keyword might happen to conflict with ones used in
    unittest itself), so I suggest this approach:

    def localfun(): return myObject.new(** stuff)
    self.failUnless Raises(Exceptio n, localfun)

    note that here I'm passing the function object itself, NOT the result of
    calling it -- there IS quite a difference!


    Alex

    Comment

    Working...