/usr/share/doc/python-pytest-doc/html/capture.html is in python-pytest-doc 2.6.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | <!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.6.2',
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.6.2</a> »</li>
<li><a href="apiref.html" accesskey="U">pytest reference documentation</a> »</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 “null” 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">"setting up </span><span class="si">%s</span><span class="s">"</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"><div class="highlight"><pre>$ py.test
=========================== test session starts ============================
platform linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3
collected 2 items
test_module.py .F
================================= FAILURES =================================
________________________________ test_func2 ________________________________
def test_func2():
> assert False
E assert False
test_module.py:9: AssertionError
-------------------------- Captured stdout setup ---------------------------
setting up <function test_func2 at 0x2b3173d7fbf8>
==================== 1 failed, 1 passed in 0.01 seconds ====================
</pre></div>
</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 "capfd" for fd-level</span>
<span class="k">print</span> <span class="p">(</span><span class="s">"hello"</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">"world</span><span class="se">\n</span><span class="s">"</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">"hello</span><span class="se">\n</span><span class="s">"</span>
<span class="k">assert</span> <span class="n">err</span> <span class="o">==</span> <span class="s">"world</span><span class="se">\n</span><span class="s">"</span>
<span class="k">print</span> <span class="s">"next"</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">"next</span><span class="se">\n</span><span class="s">"</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’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/hpk42/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/hpk42/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">
© Copyright 2014, holger krekel.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
</div>
</body>
</html>
|