Perhaps a good possible solution is to simply write the code so that it can easily test it. In the case of "diff," this seems easy enough (not having a lot of other contexts, admittedly) to use diff as arguments to already-open file objects. This is probably a pretty small change in the code, and makes testing very simple, since you can easily provide mock file objects for diff () when you test it, instead of trying to jump over hoops, mocking two instances of the same the built-in function as a context manager, called inside ... itself ... or something; -)
import StringIO diff(a, b): oldtext = a.read() newtext = b.read() def test_diff(): a = StringIO.StringIO('text1') b = StringIO.StringIO('text2') res = diff(a, b) <some assertion here>
Will this work for your business?
source share