Firstly, I'd expect this to delete an attribute too, if the original
was unset; this doesn't, but the tests don't make it clear whether
that is oversight or intent.
Secondly, this is so useful, I really would rather not see it on TestCase.
How about
def patch(obj, attribute, value):
"""Set obj.attribute to value and return a cleanup to restore it."""
?
Then you can do
self.addCleanup(patch(sys, "stderr", StringIO())
A few thoughts.
Firstly, I'd expect this to delete an attribute too, if the original
was unset; this doesn't, but the tests don't make it clear whether
that is oversight or intent.
Secondly, this is so useful, I really would rather not see it on TestCase.
How about
def patch(obj, attribute, value):
"""Set obj.attribute to value and return a cleanup to restore it."""
?
Then you can do (patch( sys, "stderr", StringIO())
self.addCleanup