[TIP] RFC: Post-assertRaises assertions WAS: ANN: unittest2 0.4.0 Release

Olemis Lang olemis at gmail.com
Fri Apr 9 06:55:58 PDT 2010


On Fri, Apr 9, 2010 at 8:15 AM, Michael Foord <fuzzyman at voidspace.org.uk> wrote:
> On 09/04/2010 14:10, Olemis Lang wrote:
>> On Thu, Apr 8, 2010 at 5:42 PM, Michael Foord<michael at voidspace.org.uk>
>>  wrote:
>>
>>> Hello all,
>>
>> :o)
>>
>>> I'm pleased to announce the release of version 0.4.0 of unittest2.
>>
>> [...]
>>
>>> For full details of all the new features, see my blog entry:
>>>
>>> *
>>> http://www.voidspace.org.uk/python/weblog/arch_d7_2010_04_03.shtml#e1171
>>>
>>
>> I wanted to know if you have considered support for writing assertions
>> targeting the exception object been raised . In my experience that is
>> very helpful when testing some kind of systems. For instance, in Trac
>> XmlRpcPlugin (<=0.1.6, current trunk ;o) you can find many tests
>> (actually all TCs checking for error conditions ;o) like this
>
> Did you check the docs?
>

Good RTFM , thnx . Should be something like this (isn't it ?):

{{{
#!python

# blah blah blah ...
   def test_wrong_argspec(self):
       ctx = self.assertRaises(xmlrpclib.Fault)
       with ctx :
           self.admin.system.listMethods("hello")
           self.fail("Oops. Wrong argspec accepted???")
       e = ctx.exception
       self.assertEquals(1, e.faultCode)
       self.assertTrue("listMethods() takes exactly 2 arguments" \
                                   in e.faultString)
}}}

However I'm also interested in how it works (backwards compatibility
;o) for versions with no support for context managers (AFAIK, there
are still people using Trac on Py 2.4 ;o)

Is it possible to consider returning the exception object when the
ctxman is not returned ?

> http://docs.python.org/dev/library/unittest.html#unittest.TestCase.assertRaises
> http://www.voidspace.org.uk/python/articles/unittest2.shtml#assertraises
>

Thnx for the pointer

:o)

PS: I read the comment about Guido's msg and I really don't agree with
«I can see how it's sometimes more powerful, but I'd say that in many
cases assertRaisesWithMessage will be easier to write and read.»
mainly because in the concrete use case I mentioned before that
approach is definitely worthless since we need to check other attrs
not just exception message, and we would need to override
`assertRaises` anyway (i.e. I see no reason for not using the more
generic approach )

:-/

-- 
Regards,

Olemis.

Blog ES: http://simelo-es.blogspot.com/
Blog EN: http://simelo-en.blogspot.com/

Featured article:
TracRpc: API v2: Fixed JSON-RPC (multicall & IDs on error). Exception
handling. - http://bitbucket.org/osimons/trac-rpc-mq/changeset/b9552972b5c3/



More information about the testing-in-python mailing list