/usr/share/doc/python-pytest/html/capture.html is in python-pytest-doc 3.3.2-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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | <!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 — pytest documentation</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: '3.3',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</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="search" title="Search" href="search.html" />
<link rel="next" title="Warnings Capture" href="warnings.html" />
<link rel="prev" title="Temporary directories and files" href="tmpdir.html" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9">
</head>
<body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="warnings.html" title="Warnings Capture"
accesskey="N">next</a></li>
<li class="right" >
<a href="tmpdir.html" title="Temporary directories and files"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="contents.html">pytest-3.3</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<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 <code class="docutils literal"><span class="pre">stdout</span></code> and <code class="docutils literal"><span class="pre">stderr</span></code> 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, <code class="docutils literal"><span class="pre">stdin</span></code> 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 <code class="docutils literal"><span class="pre">pytest</span></code> 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><code class="docutils literal"><span class="pre">sys</span></code> level capturing: Only writes to Python files <code class="docutils literal"><span class="pre">sys.stdout</span></code>
and <code class="docutils literal"><span class="pre">sys.stderr</span></code> 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-default"><div class="highlight"><pre><span></span><span class="n">pytest</span> <span class="o">-</span><span class="n">s</span> <span class="c1"># disable all capturing</span>
<span class="n">pytest</span> <span class="o">--</span><span class="n">capture</span><span class="o">=</span><span class="n">sys</span> <span class="c1"># replace sys.stdout/stderr with in-mem files</span>
<span class="n">pytest</span> <span class="o">--</span><span class="n">capture</span><span class="o">=</span><span class="n">fd</span> <span class="c1"># 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-default"><div class="highlight"><pre><span></span><span class="c1"># 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="nb">print</span> <span class="p">(</span><span class="s2">"setting up </span><span class="si">%s</span><span class="s2">"</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="kc">True</span>
<span class="k">def</span> <span class="nf">test_func2</span><span class="p">():</span>
<span class="k">assert</span> <span class="kc">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-default"><div class="highlight"><pre><span></span>$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
rootdir: $REGENDOC_TMPDIR, inifile:
collected 2 items
test_module.py .F [100%]
================================= 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 0xdeadbeef>
==================== 1 failed, 1 passed in 0.12 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 <code class="docutils literal"><span class="pre">capsys</span></code>, <code class="docutils literal"><span class="pre">capsysbinary</span></code>, <code class="docutils literal"><span class="pre">capfd</span></code>, and <code class="docutils literal"><span class="pre">capfdbinary</span></code> fixtures
allow access to 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></span><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="c1"># or use "capfd" for fd-level</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"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="s2">"world</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="n">captured</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">captured</span><span class="o">.</span><span class="n">out</span> <span class="o">==</span> <span class="s2">"hello</span><span class="se">\n</span><span class="s2">"</span>
<span class="k">assert</span> <span class="n">captured</span><span class="o">.</span><span class="n">err</span> <span class="o">==</span> <span class="s2">"world</span><span class="se">\n</span><span class="s2">"</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"next"</span><span class="p">)</span>
<span class="n">captured</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">captured</span><span class="o">.</span><span class="n">out</span> <span class="o">==</span> <span class="s2">"next</span><span class="se">\n</span><span class="s2">"</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">readouterr()</span></code> call snapshots the output so far -
and capturing will be continued. After the test
function finishes the original streams will
be restored. Using <code class="docutils literal"><span class="pre">capsys</span></code> 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 <code class="docutils literal"><span class="pre">capfd</span></code> fixture 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 class="versionadded">
<p><span class="versionmodified">New in version 3.3.</span></p>
</div>
<p>The return value from <code class="docutils literal"><span class="pre">readouterr</span></code> changed to a <code class="docutils literal"><span class="pre">namedtuple</span></code> with two attributes, <code class="docutils literal"><span class="pre">out</span></code> and <code class="docutils literal"><span class="pre">err</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.3.</span></p>
</div>
<p>If the code under test writes non-textual data, you can capture this using
the <code class="docutils literal"><span class="pre">capsysbinary</span></code> fixture which instead returns <code class="docutils literal"><span class="pre">bytes</span></code> from
the <code class="docutils literal"><span class="pre">readouterr</span></code> method. The <code class="docutils literal"><span class="pre">capfsysbinary</span></code> fixture is currently only
available in python 3.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.3.</span></p>
</div>
<p>If the code under test writes non-textual data, you can capture this using
the <code class="docutils literal"><span class="pre">capfdbinary</span></code> fixture which instead returns <code class="docutils literal"><span class="pre">bytes</span></code> from
the <code class="docutils literal"><span class="pre">readouterr</span></code> method. The <code class="docutils literal"><span class="pre">capfdbinary</span></code> fixture operates on the
filedescriptor level.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.0.</span></p>
</div>
<p>To temporarily disable capture within a test, both <code class="docutils literal"><span class="pre">capsys</span></code>
and <code class="docutils literal"><span class="pre">capfd</span></code> have a <code class="docutils literal"><span class="pre">disabled()</span></code> method that can be used
as a context manager, disabling capture inside the <code class="docutils literal"><span class="pre">with</span></code> block:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">test_disabling_capturing</span><span class="p">(</span><span class="n">capsys</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s1">'this output is captured'</span><span class="p">)</span>
<span class="k">with</span> <span class="n">capsys</span><span class="o">.</span><span class="n">disabled</span><span class="p">():</span>
<span class="k">print</span><span class="p">(</span><span class="s1">'output not captured, going directly to sys.stdout'</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s1">'this output is also captured'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<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>
<li><a href="backwards-compatibility.html">Backwards Compatibility</a></li>
<li><a href="license.html">License</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>Previous: <a href="tmpdir.html" title="previous chapter">Temporary directories and files</a></li>
<li>Next: <a href="warnings.html" title="next chapter">Warnings Capture</a></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://github.com/pytest-dev/pytest/">pytest @ GitHub</a></li>
<li><a href="http://plugincompat.herokuapp.com/">3rd party plugins</a></li>
<li><a href="https://github.com/pytest-dev/pytest/issues">Issue Tracker</a></li>
<li><a href="https://media.readthedocs.org/pdf/pytest/latest/pytest.pdf">PDF Documentation</a>
</ul>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<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>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
© Copyright 2018, holger krekel and pytest-dev team.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
</div>
</body>
</html>
|