title: How do I stub a class in a module in Python for testing?
body: <p>I have a module I am using which uses RealClass, so it is an internal dependency I don't have access to.</p>
  
  <p>I want to be able to create a FakeClass which replaces the functionality of the RealClass for testing.  I don't want to replace individual methods but the entire class.</p>
  
  <p>I looked at <a href="http://www.reahl.org/project?name=stubble" rel="nofollow">stubble</a> which seems to be what I want but I was wondering if mox or any of the other mocking frameworks have this functionality?  Or what would you suggest to use?  Maybe <a href="http://farmdev.com/projects/fudge/api/fudge.html" rel="nofollow">fudge</a>, monkey-patching?  Just looking for best practices with this stuff.  Also any useful examples would be awesome.</p>
  
  <p>Pseudo code:</p>
  
  <pre><code>from module import RealClass
  
  class FakeClass
      methodsFromRealClassOverridden
  
  class Test(unittest.TestCase):
      setup()
      teardown()
  
  test1()
      stub(RealClass, FakeClass) // something like this, but really just want the functionality
      classThatUsesRealClass // now will use FakeClass
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Here's one way I found to do it. It isn't perfect but it works.</p>
  
  <p>Example:</p>
  
  <pre><code>fake = FakeClass()
  stub = stubout.StubOutForTesting()
  stub.Set(RealClass, 'method_1', fake.method_1)
  stub.Set(RealClass, 'method_2', fake.method_2)
  </code></pre>