title: Mocking a subprocess call in Python
body: <p>I have a method - <code>run_script()</code> - I would like to test. Specifically, I want to test that a call to <a href="https://docs.python.org/3/library/subprocess.html#subprocess.Popen" rel="nofollow noreferrer"><code>subprocess.Popen</code></a> occurs. It would be even better to test that <code>subprocess.Popen</code> is called with certain parameters. When I run the test however I get <code>TypeError: 'tuple' object is not callable</code>.</p>
  <p>How can I test my method to ensure that <code>subprocess</code> is actually being called using mocks?</p>
  <pre class="lang-py prettyprint-override"><code>@mock.patch(&quot;subprocess.Popen&quot;)
  def run_script(file_path):
      process = subprocess.Popen([&quot;myscript&quot;, -M, file_path], stdout=subprocess.PIPE)
      output, err = process.communicate()
      return process.returncode
  
  
  def test_run_script(self, mock_subproc_popen):
      mock_subproc_popen.return_value = mock.Mock(
          communicate=(&quot;ouput&quot;, &quot;error&quot;), returncode=0
      )
      am.account_manager(&quot;path&quot;)
      self.assertTrue(mock_subproc_popen.called)
  </code></pre>