title: How to replace a class?
body: <p>I am writing test files for a Python module </p>
  
  <pre><code>class A:
      def func(self):
          B().sub_func()
  class B:
      def sub_func(self):
          pass
  </code></pre>
  
  <p>and I need block B's side effect while testing A.
  my question is that how to replace class B in test file.</p>
  
  <pre><code>class ATest(unittest.TestCase):
      def test_a(self):
          a=A()
          a.func()
          #now object a will invoke a func that B is a mock class
  </code></pre>
  
  <p>following the instruction of accepted answer. I finish my tests </p>
  
  <p>tws/main.py</p>
  
  <pre><code>class A(object):
      def func(self):
          b=B()
          print('func ')
          b.sub_func()
  class B(object):
      def sub_func(self):
          print('real sub')
          c=C()
          c.c_sub_func()
  class C(object):
      def c_sub_func(self):
          print('c')
  </code></pre>
  
  <p>test/test_mock.py</p>
  
  <pre><code>import unittest
  from unittest.mock import patch
  from tws.main import A
  class B():
      def sub_func(self):
          print('mock')
          return 12
  class TestMock(unittest.TestCase):
      @patch('tws.main.B',new=B)
      def test_af(self):
          a=A()
          print(a.__dict__)
          a.func()
          print('rrr')
  </code></pre>
  
  <p>it will print mock,hope it will help other people with same problems.</p>