[TIP] About assertMultilineEqual

Jorge Vargas jorge.vargas at gmail.com
Tue Aug 17 12:04:56 PDT 2010

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):

in your setup.py and then on your tests you will do
    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

More information about the testing-in-python mailing list