'Helper method to patch an object and automatically cleanup.'

def autopatch(self, obj, attr, *a, **kw):

    p = patch.object(obj, attr, *a, **kw)
    m = p.start()
    self.addCleanup(p.stop)
    return m
