title: Mocking python subprocess.call function and capture its system exit code
body: <p>Writing test cases to handle successful and failed python subprocess calls, I need to capture <code>subprocess.call</code> returning code.</p>
  
  <p>Using python <code>unittest.mock module</code>, is it possible to patch the <code>subprocess.call</code> function and capture its real system exit code?</p>
  
  <p>Consider an external library with the following code:</p>
  
  <pre><code>## &lt;somemodule.py&gt; file
  
  import subprocess
  
  
  def run_hook(cmd):
      # ...
      subprocess.call(cmd, shell=True)
      sum_one = 1+1
      return sum_one
  </code></pre>
  
  <p>I can't modify the function <code>run_hook</code>. It is not part of my code. But, the fact is that <code>subprocess.call</code> is being called among other statements.</p>
  
  <p>Here we have a snippet of code returning a forced system exit error code 1:</p>
  
  <pre><code>## &lt;somemodule.py&gt; tests file
  
  import subprocess
  from somemodule import run_hook
  
  try:
      from unittest.mock import patch
  except ImportError:
      from mock import patch
  
  
  @patch("subprocess.call", side_effect=subprocess.call)
  def test_run_hook_systemexit_not_0(call_mock):
      python_exec = sys.executable
      cmd_parts = [python_exec, "-c", "'exit(1)'"] # Forced error code 1
      cmd = " ".join(cmd_parts)
      run_hook(cmd)
      call_mock.assert_called_once_with(cmd, shell=True)
  
      # I need to change the following assertion to
      # catch real return code "1"
  
      assert "1" == call_mock.return_value(), \
             "Forced system exit(1) should return 1. Just for example purpose"
  </code></pre>
  
  <p>How can I improve this test to capture the expected real value for any <code>subprocess.call</code> return code?</p>
  
  <p>For compatibility purposes, new <code>subprocess.run</code> (3.5+) can't be used. This library is still broadly used by python 2.7+ environments.</p>