Current File : //usr/share/doc/pytest-2.7.0/html/en/capture.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Capturing of the stdout/stderr output</title>
    
    <link rel="stylesheet" href="_static/flasky.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '2.7.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="shortcut icon" href="_static/pytest1favi.ico"/>
    <link rel="top" title="None" href="index.html" />
    <link rel="up" title="pytest reference documentation" href="apiref.html" />
    <link rel="next" title="Monkeypatching/mocking modules and environments" href="monkeypatch.html" />
    <link rel="prev" title="classic xunit-style setup" href="xunit_setup.html" />
   
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9">

  </head>
  <body>
  
  

    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="monkeypatch.html" title="Monkeypatching/mocking modules and environments"
             accesskey="N">next</a></li>
        <li class="right" >
          <a href="xunit_setup.html" title="classic xunit-style setup"
             accesskey="P">previous</a> |</li>
        <li><a href="contents.html">pytest-2.7.0</a> &raquo;</li>
          <li><a href="apiref.html" accesskey="U">pytest reference documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="capturing-of-the-stdout-stderr-output">
<span id="captures"></span><h1>Capturing of the stdout/stderr output<a class="headerlink" href="#capturing-of-the-stdout-stderr-output" title="Permalink to this headline">¶</a></h1>
<div class="section" id="default-stdout-stderr-stdin-capturing-behaviour">
<h2>Default stdout/stderr/stdin capturing behaviour<a class="headerlink" href="#default-stdout-stderr-stdin-capturing-behaviour" title="Permalink to this headline">¶</a></h2>
<p>During test execution any output sent to <tt class="docutils literal"><span class="pre">stdout</span></tt> and <tt class="docutils literal"><span class="pre">stderr</span></tt> is
captured.  If a test or a setup method fails its according captured
output will usually be shown along with the failure traceback.</p>
<p>In addition, <tt class="docutils literal"><span class="pre">stdin</span></tt> is set to a &#8220;null&#8221; object which will
fail on attempts to read from it because it is rarely desired
to wait for interactive input when running automated tests.</p>
<p>By default capturing is done by intercepting writes to low level
file descriptors.  This allows to capture output from simple
print statements as well as output from a subprocess started by
a test.</p>
</div>
<div class="section" id="setting-capturing-methods-or-disabling-capturing">
<h2>Setting capturing methods or disabling capturing<a class="headerlink" href="#setting-capturing-methods-or-disabling-capturing" title="Permalink to this headline">¶</a></h2>
<p>There are two ways in which <tt class="docutils literal"><span class="pre">pytest</span></tt> can perform capturing:</p>
<ul class="simple">
<li>file descriptor (FD) level capturing (default): All writes going to the
operating system file descriptors 1 and 2 will be captured.</li>
<li><tt class="docutils literal"><span class="pre">sys</span></tt> level capturing: Only writes to Python files <tt class="docutils literal"><span class="pre">sys.stdout</span></tt>
and <tt class="docutils literal"><span class="pre">sys.stderr</span></tt> will be captured.  No capturing of writes to
filedescriptors is performed.</li>
</ul>
<p id="disable-capturing">You can influence output capturing mechanisms from the command line:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">py</span><span class="o">.</span><span class="n">test</span> <span class="o">-</span><span class="n">s</span>            <span class="c"># disable all capturing</span>
<span class="n">py</span><span class="o">.</span><span class="n">test</span> <span class="o">--</span><span class="n">capture</span><span class="o">=</span><span class="n">sys</span> <span class="c"># replace sys.stdout/stderr with in-mem files</span>
<span class="n">py</span><span class="o">.</span><span class="n">test</span> <span class="o">--</span><span class="n">capture</span><span class="o">=</span><span class="n">fd</span>  <span class="c"># also point filedescriptors 1 and 2 to temp file</span>
</pre></div>
</div>
</div>
<div class="section" id="using-print-statements-for-debugging">
<span id="printdebugging"></span><h2>Using print statements for debugging<a class="headerlink" href="#using-print-statements-for-debugging" title="Permalink to this headline">¶</a></h2>
<p>One primary benefit of the default capturing of stdout/stderr output
is that you can use print statements for debugging:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># content of test_module.py</span>

<span class="k">def</span> <span class="nf">setup_function</span><span class="p">(</span><span class="n">function</span><span class="p">):</span>
    <span class="k">print</span> <span class="p">(</span><span class="s">&quot;setting up </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">function</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">test_func1</span><span class="p">():</span>
    <span class="k">assert</span> <span class="bp">True</span>

<span class="k">def</span> <span class="nf">test_func2</span><span class="p">():</span>
    <span class="k">assert</span> <span class="bp">False</span>
</pre></div>
</div>
<p>and running this module will show you precisely the output
of the failing function and hide the other one:</p>
<div class="highlight-python"><pre>$ py.test
=========================== test session starts ============================
platform linux -- Python 3.4.0 -- py-1.4.26 -- pytest-2.7.0
rootdir: /tmp/doc-exec-101, inifile:
collected 2 items

test_module.py .F

================================= FAILURES =================================
________________________________ test_func2 ________________________________

    def test_func2():
&gt;       assert False
E       assert False

test_module.py:9: AssertionError
-------------------------- Captured stdout setup ---------------------------
setting up &lt;function test_func2 at 0x2b24d5259158&gt;
==================== 1 failed, 1 passed in 0.01 seconds ====================</pre>
</div>
</div>
<div class="section" id="accessing-captured-output-from-a-test-function">
<h2>Accessing captured output from a test function<a class="headerlink" href="#accessing-captured-output-from-a-test-function" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">capsys</span></tt> and <tt class="docutils literal"><span class="pre">capfd</span></tt> fixtures allow to access stdout/stderr
output created during test execution.  Here is an example test function
that performs some output related checks:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">test_myoutput</span><span class="p">(</span><span class="n">capsys</span><span class="p">):</span> <span class="c"># or use &quot;capfd&quot; for fd-level</span>
    <span class="k">print</span> <span class="p">(</span><span class="s">&quot;hello&quot;</span><span class="p">)</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;world</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
    <span class="n">out</span><span class="p">,</span> <span class="n">err</span> <span class="o">=</span> <span class="n">capsys</span><span class="o">.</span><span class="n">readouterr</span><span class="p">()</span>
    <span class="k">assert</span> <span class="n">out</span> <span class="o">==</span> <span class="s">&quot;hello</span><span class="se">\n</span><span class="s">&quot;</span>
    <span class="k">assert</span> <span class="n">err</span> <span class="o">==</span> <span class="s">&quot;world</span><span class="se">\n</span><span class="s">&quot;</span>
    <span class="k">print</span> <span class="s">&quot;next&quot;</span>
    <span class="n">out</span><span class="p">,</span> <span class="n">err</span> <span class="o">=</span> <span class="n">capsys</span><span class="o">.</span><span class="n">readouterr</span><span class="p">()</span>
    <span class="k">assert</span> <span class="n">out</span> <span class="o">==</span> <span class="s">&quot;next</span><span class="se">\n</span><span class="s">&quot;</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">readouterr()</span></tt> call snapshots the output so far -
and capturing will be continued.  After the test
function finishes the original streams will
be restored.  Using <tt class="docutils literal"><span class="pre">capsys</span></tt> this way frees your
test from having to care about setting/resetting
output streams and also interacts well with pytest&#8217;s
own per-test capturing.</p>
<p>If you want to capture on filedescriptor level you can use
the <tt class="docutils literal"><span class="pre">capfd</span></tt> function argument which offers the exact
same interface but allows to also capture output from
libraries or subprocesses that directly write to operating
system level output streams (FD1 and FD2).</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="contents.html">
              <img class="logo" src="_static/pytest1.png" alt="Logo"/>
            </a></p><h3><a href="contents.html">Table Of Contents</a></h3>

<ul>
  <li><a href="index.html">Home</a></li>
  <li><a href="contents.html">Contents</a></li>
  <li><a href="getting-started.html">Install</a></li>
  <li><a href="example/index.html">Examples</a></li>
  <li><a href="customize.html">Customize</a></li>
  <li><a href="contact.html">Contact</a></li>
  <li><a href="talks.html">Talks/Posts</a></li>
  <li><a href="changelog.html">Changelog</a></li>
</ul>
  <hr>
  <ul>
<li><a class="reference internal" href="#">Capturing of the stdout/stderr output</a><ul>
<li><a class="reference internal" href="#default-stdout-stderr-stdin-capturing-behaviour">Default stdout/stderr/stdin capturing behaviour</a></li>
<li><a class="reference internal" href="#setting-capturing-methods-or-disabling-capturing">Setting capturing methods or disabling capturing</a></li>
<li><a class="reference internal" href="#using-print-statements-for-debugging">Using print statements for debugging</a></li>
<li><a class="reference internal" href="#accessing-captured-output-from-a-test-function">Accessing captured output from a test function</a></li>
</ul>
</li>
</ul>
<h3>Related Topics</h3>
<ul>
  <li><a href="contents.html">Documentation overview</a><ul>
  <li><a href="apiref.html">pytest reference documentation</a><ul>
      <li>Previous: <a href="xunit_setup.html" title="previous chapter">classic xunit-style setup</a></li>
      <li>Next: <a href="monkeypatch.html" title="next chapter">Monkeypatching/mocking modules and environments</a></li>
  </ul></li>
  </ul></li>
</ul><h3>Useful Links</h3>
<ul>
  <li><a href="index.html">The pytest Website</a></li>
  <li><a href="contributing.html">Contribution Guide</a></li>
  <li><a href="https://pypi.python.org/pypi/pytest">pytest @ PyPI</a></li>
  <li><a href="https://bitbucket.org/pytest-dev/pytest/">pytest @ Bitbucket</a></li>
  <li><a href="http://pytest.org/latest/plugins_index/index.html">3rd party plugins</a></li>
  <li><a href="https://bitbucket.org/pytest-dev/pytest/issues?status=new&status=open">Issue Tracker</a></li>
  <li><a href="http://pytest.org/latest/pytest.pdf">PDF Documentation</a>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>

  <div class="footer">
    &copy; Copyright 2014, holger krekel.
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
  </div>
  
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-7597274-13']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

  </body>
</html>