still not happy with unittest.failUnlessRaises

Discussion in 'Python' started by paul koelle, Oct 9, 2004.

  1. paul koelle

    paul koelle Guest

    hi folks,

    while I got my own errors kind of sorted out now (I think), unittest
    still not behaves like it should.

    code:
    print 'this is expected to fail. The "Type" column is unique in the DB'
    self.failUnlessRaises(IntegrityError, self.failhelper(certs.Subcacert,
    self.ca_client_tmpl))

    traceback:
    File "/usr/lib/python2.2/site-packages/SQLObject/DBConnection.py", line
    767, in _queryInsertID c.execute(q)
    File "/usr/lib/python2.2/site-packages/sqlite/main.py", line 243, in
    execute self.rs = self.con.db.execute(SQL)
    IntegrityError: column type is not unique

    ----------------------------------------------------------------------
    Ran 1 test in 56.245s

    FAILED (errors=1)

    the docu (2.2) says:
    failUnlessRaises( exception, callable, ...)
    ....The test passes if exception is raised, is an error if another
    exception is raised, or fails if no exception is raised.

    and:
    issubclass(sqlite.IntegrityError, Exception)
    True

    IMO the test should pass. Any ideas what could be wrong here?

    thanks
    Paul
     
    paul koelle, Oct 9, 2004
    #1
    1. Advertisements

  2. paul koelle

    Tim Peters Guest

    [paul koelle]
    By "this is expected to fail", do you mean that the test is supposed
    to fail, or that the test is supposed to pass by verifying that the
    thing it's testing fails? "This" is ambiguous. I'll assume the
    latter.
    self.ca_client_tmpl))

    What you're actually testing here is that IntegrityError gets raised by

    temp()

    where

    temp = self.failhelper(certs.Subcacert, self.ca_client_tmpl)

    is evaluated outside the control of failUnlessRaises.
    failUnlessRaises *calls* its second argument. If

    self.failhelper(certs.Subcacert, self.ca_client_tmpl)

    on its own raises IntegrityError, and that's what you intended to
    test, then you need to spell the test

    self.failUnlessRaises(IntegrityError, self.failhelper,
    certs.Subcacert, self.ca_client_tmpl)

    You didn't show enough code so that a reader could guess the answers
    to these questions.
    Yes. Note that the second argument is a callable object, and the
    '...' consists of arguments to pass to the callable. So, e.g., the
    test

    self.failUnlessRaises(ValueError, math.sqrt(-1))

    fails, but the test

    self.failUnlessRaises(ValueError, math.sqrt, -1)

    passes.
     
    Tim Peters, Oct 9, 2004
    #2
    1. Advertisements

  3. paul koelle

    paul koelle Guest

    correct.

    [ snipp ]
    My apologies. In fact, it turned out to be simple as:
    self.failUnlessRaises(IntegrityError, certs.Subcacert.new,
    **self.client_tmpl)

    thanks again
    Paul
     
    paul koelle, Oct 10, 2004
    #3
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.