title: Running Newman command with subprocess
body: <p>I have a Newman command that is part of my script. I'd like the entire script to quit (or go back to the main menu) when the collection encounters an error.</p>
  <pre><code>from subprocess import CalledProcessError, Popen, PIPE
  from io import TextIOWrapper
  
  def run_sh(command):
      process = Popen(shlex.split(command), stdout=PIPE)
      for line in TextIOWrapper(process.stdout, newline=&quot;&quot;):
          print(line, &quot;&quot;)
  
  cmd = newman run &quot;My_Collection.postman_collection.json&quot; --folder &quot;My_Folder&quot; -e ../../Postman/Environments/My_Environment.json -d &quot;CREDS.txt&quot; -r cli,csv -n 1 --reporter-csv-includeBody --reporter-csv-export ./RESPONSES.csv
  </code></pre>
  <p>The <code>run_sh(cmd)</code> is part of a bigger script. I'd like the script to not carry on to the next step if errors are encountered when running the collection. Using <code>try</code> and <code>except</code> won't work because the <code>run_sh</code> command successfully goes through (it runs the collection but the collection yields errors).</p>
  <p>Example of an error:</p>
  <pre><code>  #  failure         detail                                                
   
                                                                             
   
   1.  AssertionError  Status code is 202                                    
   
                       expected response to have status code 202 but got 401 
   
                       at assertion:0 in test-script                         
   
                       inside &quot;MY FOLDER&quot;                
  </code></pre>
  <p>What would be the best way to go about this?</p>