unittest assertRaises Problem

Discussion in 'Python' started by john, Apr 16, 2007.

  1. john

    john Guest

    All:

    Hi. I am an experienced developer (15 yrs), but new to Python and have
    a question re unittest and assertRaises. No matter what I raise,
    assertRaises is never successful. Here is the test code:


    class Foo:
    def testException(self):
    raise ValueError

    class FooTestCase(unittest.TestCase):

    testTryThis(self):
    f = Foo()
    self.assertRaises(ValueError, f.testException())

    This fails --- unittest reports the following:
    FAILED (errors=1)

    This seems like the most basic thing in the world. I am running Python
    2.5 on Windows XP using Eclipse and PyDev

    Any help appreciated.

    Thanks,
    John
    john, Apr 16, 2007
    #1
    1. Advertising

  2. john

    Guest

    On Apr 16, 3:13 pm, "john" <> wrote:
    > All:
    >
    > Hi. I am an experienced developer (15 yrs), but new to Python and have
    > a question re unittest and assertRaises. No matter what I raise,
    > assertRaises is never successful. Here is the test code:
    >
    > class Foo:
    > def testException(self):
    > raise ValueError
    >
    > class FooTestCase(unittest.TestCase):
    >
    > testTryThis(self):
    > f = Foo()
    > self.assertRaises(ValueError, f.testException())
    >



    The second argument should be a callable object, not the
    result from invoking that callable object. Thus, change

    self.assertRaises(ValueError, f.testException())

    to

    self.assertRaises(ValueError, f.testException)

    --
    Hope this helps,
    Steven
    , Apr 16, 2007
    #2
    1. Advertising

  3. john

    john Guest

    On Apr 16, 6:35 pm, Jean-Paul Calderone <> wrote:
    > On 16 Apr 2007 15:13:42 -0700, john <> wrote:
    >
    >
    >
    > >All:

    >
    > >Hi. I am an experienced developer (15 yrs), but new to Python and have
    > >a question re unittest and assertRaises. No matter what I raise,
    > >assertRaises is never successful. Here is the test code:

    >
    > >class Foo:
    > > def testException(self):
    > > raise ValueError

    >
    > >class FooTestCase(unittest.TestCase):

    >
    > > testTryThis(self):
    > > f = Foo()
    > > self.assertRaises(ValueError, f.testException())

    >
    > The 2nd argument to assertRaises should be a callable. assertRaises
    > will call it (so that it can do exception handling), so you shouldn't:
    >
    > self.assertRaises(ValueError, f.testException)
    >
    > Jean-Paul


    Steven, Jean-Paul:

    Thank you both for your answers - worked like a charm!

    Best,
    John
    john, Apr 17, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul  Moore
    Replies:
    1
    Views:
    384
    Paul Moore
    Oct 14, 2008
  2. Inyeol
    Replies:
    3
    Views:
    320
    Chris Torek
    Oct 27, 2010
  3. Steven D'Aprano

    Case study: debugging failed assertRaises bug

    Steven D'Aprano, Apr 26, 2011, in forum: Python
    Replies:
    2
    Views:
    336
    R David Murray
    Apr 27, 2011
  4. Ulrich Eckhardt
    Replies:
    20
    Views:
    808
    Terry Reedy
    Apr 2, 2012
  5. andrea crotti

    Re: assertraises behaviour

    andrea crotti, Jul 16, 2012, in forum: Python
    Replies:
    0
    Views:
    174
    andrea crotti
    Jul 16, 2012
Loading...

Share This Page