[New-bugs-announce] [issue10675] unittest should have an assertChanges context manager

Jay Moorthi report at bugs.python.org
Fri Dec 10 20:58:36 CET 2010


New submission from Jay Moorthi <moorthi at gmail.com>:

It would be useful to have a new assert method in the unittest.TestCase class that checks to see if a value has changed.  I wrote a quick and dirty version like so:

class MySpecialTestCase(unittest.TestCase):
    @contextmanager
    def assertChanges(self, thing, attr=None, by=None):
        def get_value(thing, attr):
            if callable(thing):
                value = thing()
            else:
                value = getattr(thing, attr)
            return value

        old_value = get_value(thing, attr)
        yield
        new_value = get_value(thing, attr)

        if by is None:
            self.assertNotEqual(new_value, old_value)
        else:
            self.assertEqual(new_value - old_value, by)

I'm sure something better can be done to take better advantage of the unittest module's diffing tools, etc.

----------
messages: 123745
nosy: Jay.Moorthi
priority: normal
severity: normal
status: open
title: unittest should have an assertChanges context manager
type: feature request
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10675>
_______________________________________


More information about the New-bugs-announce mailing list