title: py.test teardown see if the test failed and print subprocess output
body: <p>I want to print subprocess output in py.test teardown if the test is failed - or send it to any other human readable output. Is it possible to check if the test has failed in teardown? Any other ways to get the output of subprocess commands only during test failures?</p>
  
  <p>My code:</p>
  
  <pre><code>"""Test different scaffold operations."""
  import subprocess
  
  import pytest
  from tempfile import mkdtemp
  
  
  @pytest.fixture(scope='module')
  def app_scaffold(request) -&gt; str:
      """py.test fixture to create app scaffold."""
  
      folder = mkdtemp(prefix="websauna_test_")
  
      cmdline = ["..."]
  
      worker = subprocess.Popen(cmdline, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  
      worker.wait(timeout=5.0)
  
      if worker.returncode is not None:
          raise AssertionError("scaffold command did not properly exit: {}".format(" ".join(cmdline)))
  
      def teardown():
          worker.terminate()
          # XXX: Hard to capture this only on failure for now
          print(worker.stdout.read().decode("utf-8"))
          print(worker.stderr.read().decode("utf-8"))
  
      request.addfinalizer(teardown)
  </code></pre>