How do I patch an object so that all methods are mocked except one?
问题 I have an entry point function call it main on an object that I would like to remain unmocked, since it calls several other methods on the object: class Thing(object): def main(self): self.alpha() self.bravo() def alpha(self): self.charlie() def bravo(self): raise TypeError("Requires Internet connection!") def charlie(self): raise Exception("Bad stuff happens here!") This is pretty straight forward to mock manually: thing = Thing() thing.alpha = MagicMock() thing.bravo = MagicMock() And I can