[TIP] About assertMultilineEqual

Michael Foord fuzzyman at voidspace.org.uk
Tue Aug 17 12:07:32 PDT 2010


On 17/08/2010 22:04, Jorge Vargas wrote:
> On Tue, Aug 17, 2010 at 10:33 AM, Alfredo Deza<alfredodeza at gmail.com>  wrote:
>    
>>
>> On Tue, Aug 17, 2010 at 9:55 AM, Michael Foord<fuzzyman at voidspace.org.uk>
>> wrote:
>>      
>>> On 17/08/2010 16:53, Alfredo Deza wrote:
>>>
>>> On Tue, Aug 17, 2010 at 5:43 AM, Michael Foord<fuzzyman at voidspace.org.uk>
>>> wrote:
>>>        
>>>> On 16/08/2010 02:21, Ned Batchelder wrote:
>>>>
>>>> I had to bridge this gap for coverage.py, and ended up doing it for a few
>>>> methods, see
>>>> http://bitbucket.org/ned/coveragepy/src/tip/test/backunittest.py
>>>>
>>>> But this method bothers me: the Multiline aspect has nothing to do with
>>>> the comparison, only with the reporting of failed assertions.  Better would
>>>> be to have assertEqual be smart enough to recognize that the arguments are
>>>> multiline strings, and just do the right thing with them.
>>>>
>>>>
>>>> If you use unittest2 then you get assertMultilineEqual and smarter
>>>> assertEqual behaviour.
>>>>
>>>>          
>>> If unittest2 is meant for Python versions older than 2.7, how do you go
>>> around specifying that in a package? I usually have a:
>>> tests_require = ['nose', 'webtest']
>>> Or similar packages (e.g. pytest)
>>>
>>> unittest2 works fine with Python 2.7 (and will soon have features that are
>>> in unittest in python 3.2 but not in 2.7).
>>>
>>>        
>> ahh got it... so regardless of doing " install_requires = ['unittes2'] " in
>> Python2.7 and later, it will still work... I thought unittest2 was
>> *only* for extending compatibility to older versions of Python.
>> Thanks for the response!
>>
>>      
> In theory that works however you will have both the unitest module
> (from dist) and the unittest2 module.
>
> One way of fixing this is to have
>
> if sys.version_info<= (2,7):
>      tests_require.append('unittest2')
>
> in your setup.py and then on your tests you will do
> try:
>      import unittest2 as unittest
> except ImportError:
>      import unittest
>
> That should get you a good version in pre-2.7 and use the "buildin"
> version post 2.7
>    
Yep, this will work fine - so long as you limit yourself to features in 
unittest 0.5.1 (currently the latest stable release).

I actually tend to have a test.support module that does the conditional 
import magic and then *always* do:

     from test.support import unittest

That keeps your imports in tests a little less cluttered. It amounts to 
the same thing though.

Michael

-- 
http://www.ironpythoninaction.com/




More information about the testing-in-python mailing list