unittest assertRaises Problem

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

    #1

    unittest assertRaises Problem

    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(s elf):
    raise ValueError

    class FooTestCase(uni ttest.TestCase) :

    testTryThis(sel f):
    f = Foo()
    self.assertRais es(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

  • attn.steven.kuo@gmail.com

    #2
    Re: unittest assertRaises Problem

    On Apr 16, 3:13 pm, "john" <puop...@gmail. comwrote:
    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(s elf):
    raise ValueError
    >
    class FooTestCase(uni ttest.TestCase) :
    >
    testTryThis(sel f):
    f = Foo()
    self.assertRais es(ValueError, f.testException ())
    >

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

    self.assertRais es(ValueError, f.testException ())

    to

    self.assertRais es(ValueError, f.testException )

    --
    Hope this helps,
    Steven

    Comment

    • john

      #3
      Re: unittest assertRaises Problem

      On Apr 16, 6:35 pm, Jean-Paul Calderone <exar...@divmod .comwrote:
      On 16 Apr 2007 15:13:42 -0700, john <puop...@gmail. comwrote:
      >
      >
      >
      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(s elf):
      raise ValueError
      >
      class FooTestCase(uni ttest.TestCase) :
      >
      testTryThis(sel f):
      f = Foo()
      self.assertRais es(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.assertRais es(ValueError, f.testException )
      >
      Jean-Paul
      Steven, Jean-Paul:

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

      Best,
      John

      Comment

      Working...