Sophie

Sophie

distrib > Mandriva > 2010.2 > i586 > media > contrib-backports > by-pkgid > 85556a06d0b40546e6bb4676359ced2e > files > 176

python-pytest-2.2.4-1mdv2010.1.noarch.rpm



<!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/sphinxdoc.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.2.4',
        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="top" title="None" href="index.html" />
    <link rel="up" title="py.test reference documentation" href="apiref.html" />
    <link rel="next" title="Monkeypatching/mocking modules and environments" href="monkeypatch.html" />
    <link rel="prev" title="Extended xUnit style setup fixtures" href="xunit_setup.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a></li>
        <li class="right" >
          <a href="monkeypatch.html" title="Monkeypatching/mocking modules and environments"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="xunit_setup.html" title="Extended xUnit style setup fixtures"
             accesskey="P">previous</a> |</li>
        <li><a href="contents.html">pytest-2.2.4</a> &raquo;</li>
          <li><a href="apiref.html" accesskey="U">py.test reference documentation</a> &raquo;</li>
 
<g:plusone></g:plusone>

      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>

<h3>quicklinks</h3>
<div style="text-align: left; font-size: 100%; vertical-align: middle;">
<table>
<tr>
<td>
        <a href="index.html">home</a>
</td><td>
        <a href="contents.html">TOC/contents</a>
</td></tr><tr><td>
        <a href="getting-started.html">install</a>
</td><td>
        <a href="changelog.html">changelog</a>
</td></tr><tr><td>
        <a href="example/index.html">examples</a>
</td><td>
        <a href="customize.html">customize</a>
</td></tr><tr><td>
        <a href="https://bitbucket.org/hpk42/pytest/issues?status=new&status=open">issues[bb]</a>
</td><td>
        <a href="contact.html">contact</a>
</td></tr></table>
</div>

  <h3><a href="contents.html">Table Of Contents</a></h3>
  <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>

  <h4>Previous topic</h4>
  <p class="topless"><a href="xunit_setup.html"
                        title="previous chapter">Extended xUnit style setup fixtures</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="monkeypatch.html"
                        title="next chapter">Monkeypatching/mocking modules and environments</a></p>
        </div>
      </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">py.test</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 darwin -- Python 2.7.1 -- pytest-2.2.2
collecting ... 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 ------------------------------
setting up &lt;function test_func2 at 0x1013230c8&gt;
==================== 1 failed, 1 passed in 0.03 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 <a class="reference internal" href="funcargs.html#funcarg-mechanism"><em>Dependency injection through function arguments</em></a> allows test function a very easy
way to access the captured output by simply using the names
<tt class="docutils literal"><span class="pre">capsys</span></tt> or <tt class="docutils literal"><span class="pre">capfd</span></tt> in the test function signature.  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 py.test&#8217;s
own per-test capturing.</p>
<p>If you want to capture on <tt class="docutils literal"><span class="pre">fd</span></tt> level you can use
the <tt class="docutils literal"><span class="pre">capfd</span></tt> function argument which offers the exact
same interface.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a></li>
        <li class="right" >
          <a href="monkeypatch.html" title="Monkeypatching/mocking modules and environments"
             >next</a> |</li>
        <li class="right" >
          <a href="xunit_setup.html" title="Extended xUnit style setup fixtures"
             >previous</a> |</li>
        <li><a href="contents.html">pytest-2.2.4</a> &raquo;</li>
          <li><a href="apiref.html" >py.test reference documentation</a> &raquo;</li>
 
<g:plusone></g:plusone>

      </ul>
    </div>

    <div class="footer">
        &copy; Copyright 2011, holger krekel et alii.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </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>
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>

  </body>
</html>