title: pytest capsys: checking output AND getting it reported?
body: <p>Python 3.4.1, pytest 2.6.2.</p>
  
  <p>When a test fails, <code>pytest</code> will routinely report what was printed to stdout by the test.
  For instance this code:</p>
  
  <pre><code>def method_under_test():
      print("Hallo, Welt!")
      return 41
  
  def test_result_only():
      result = method_under_test()
      assert result == 42
  </code></pre>
  
  <p>when executed as <code>python -m pytest myfile.py</code>,
  will report this:</p>
  
  <pre><code>================================== FAILURES ===================================
  ______________________________ test_result_only _______________________________
  
      def test_result_only():
          result = method_under_test()
  &gt;       assert result == 42
  E       assert 41 == 42
  
  pytestest.py:9: AssertionError
  ---------------------------- Captured stdout call -----------------------------
  Hallo, Welt!
  ========================== 1 failed in 0.03 seconds ===========================
  </code></pre>
  
  <p>This is a very nice feature.
  But when I use pytest's built-in <code>capsys</code> fixture, like this:</p>
  
  <pre><code>def test_result_and_stdout(capsys):
      result = method_under_test()
      out, err = capsys.readouterr()
      assert out.startswith("Hello")
      assert result == 42
  </code></pre>
  
  <p>the report no longer contains the actual output:</p>
  
  <pre><code>================================== FAILURES ===================================
  ___________________________ test_result_and_stdout ____________________________
  
  capsys = &lt;_pytest.capture.CaptureFixture object at 0x000000000331FB70&gt;
  
      def test_result_and_stdout(capsys):
          result = method_under_test()
          out, err = capsys.readouterr()
  &gt;       assert out.startswith("Hello")
  E       assert &lt;built-in method startswith of str object at 0x000000000330C3B0&gt;('Hello')
  E        +  where &lt;built-in method startswith of str object at 0x000000000330C3B0&gt; = 'Hallo, Welt!\n'.startswith
  
  pytestest.py:14: AssertionError
  ========================== 1 failed in 0.03 seconds ===========================
  </code></pre>
  
  <p>I am not sure whether this behavior is according to specification;
  the <a href="http://pytest.org/latest/capture.html#accessing-captured-output-from-a-test-function" rel="noreferrer">pytest documentation</a> says about <code>readouterr</code>:
  "After the test function finishes
  the original streams will be restored."</p>
  
  <p>I have tried assuming <code>capsys</code> is a context manager and have 
  called <code>capsys.__exit__()</code> just before the asserts.
  This would be an ugly solution, but at least a solution if
  it restored the output before my assertion.
  However, this only produces</p>
  
  <pre><code>AttributeError: 'CaptureFixture' object has no attribute '__exit__'
  </code></pre>
  
  <p>Next I looked into the <code>CaptureFixture</code> class source code
  and found a promising-looking method <code>close</code> (which calls
  some <code>pop_outerr_to_orig()</code> method),
  but calling <code>capsys.close()</code> in my test did not help either,
  it had no obvious effect at all.</p>
  
  <p>How can I get pytest to report my outputs upon failure
  in a test using <code>capsys</code>?</p>