title: Unittest: mock a python class with init method
body: <p>I am trying to test a  run method in my class which has init method and takes object as parameter from another class:</p>
  <pre><code>
  class ServePlate(FreeSurferStep):
      process_name = &quot;FreeSurfer&quot;
      step_name = &quot;ServePlate&quot;
      step_cli = &quot;serve&quot;
      cpu = 1
      mem = 1024
  
      def __init__(self, project, code, args):
          super(Stage, self).__init__(project, code, args)
          self.next_step = Autorecon1
  
      @classmethod
      def get_queue(cls, project_name):
          plog = ProcessingLog()
          available = plog.get_project_images(project_name, &quot;T1&quot;)
          attempted = plog.get_step_attempted(project_name, cls.process_name, cls.step_name)
          attempted_codes = [row.Code for row in attempted]
          todo = [{'ProjectName': project_name, 'Code': row.Code} for row in available if row.Code not in attempted_codes]
          return todo
  
      def run(self):                        #&lt;&lt;&lt;&lt;-- This method is to be tested
  
          source = None
          image = ProcessingLog().get_project_image(self.project, self.code)
          if image.ImageStore == &quot;Dicom&quot;:
              dcmtmp = tempfile.mkdtemp()
              DicomRepository().fetch_dicoms(self.code, dcmtmp)
  
              first_t1 = os.path.join(dcmtmp, os.listdir(dcmtmp)[0])
              niitmp = os.path.join(tempfile.mkdtemp(), 'raw.nii')
              cmd = 'dcm2niix -b n -z n -g i -o {} -f raw {}'.format(os.path.dirname(niitmp), first_t1)
              self._run_fs_cmd(cmd)
  
              source = niitmp
          elif image.ImageStore == &quot;Pre-Processed&quot;:
              source = [PreProcessedImageRepository().get_image(self.code), ]
  
          if source is None:
              raise ProcessingError(&quot;Could not find staging data.&quot;)
  
          first_t1 = self._copy_files(source)
  
          cmd = 'recon-all -s %(code)s -i %(image)s' % {
              &quot;code&quot;: self.code,
              &quot;image&quot;: first_t1
          }
          self._run_fs_cmd(cmd).                  #&lt;&lt;&lt;-- I am trying to check value of cmd variable
  </code></pre>
  <p>Here is my test, i am patching first the init method and second _run_fs_cmd frm another class.</p>
  <pre><code>class Testfs(unittest.TestCase):
  
      @patch.object(fs.FreeSurferStep, '_run_fs_cmd', spec=True)
      # @patch.object(fs.FreeSurferStep, '__init__')
      def test_serve(mock_serve):
          &quot;&quot;&quot;
          Serve step test
          &quot;&quot;&quot;
          mock_serve.project = 'TEST_FS'
          mock_serve.code = 'Test9001-1a5'
          mock_serve.args = ''
          mock_stage.return_value = None
          FsObj = FreeSurferStep('serve')
          stage_obj = Stage(FsObj)
          FsObj.run()
          #
          # stage_obj.run(self)
          #
          # self.assertEqual(self.cmd, '')
          # fs.FreeSurferStep._run_fs_cmd = Mock()
  </code></pre>
  <p>this gives me error. Here even though i am passing no arguments to the run method, it keeps on complaining about more argument being passed. Also patching a class object to be passed to ServePlate method and patching <em>run_fsmethod</em> where the cmd is passed to doesn't seem to work. Do i need to compulsorily mock all other methods being called?<br />
  <code>TypeError: test_serve() takes 1 positional argument but 2 were given</code>
  <code>TypeError: run() takes 1 positional argument but 3 were given</code></p>