1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<meta content="text/html;charset=ISO-8859-1" name="Content-Type"/>
6
<link href="../../style.css" media="screen" rel="stylesheet" type="text/css"/></head>
9
<div><a href="http://pylib.org"><img alt="py lib" height="57" id="pyimg" src="http://codespeak.net/img/pylib.png" width="77"/></a></div>
12
<div><a class="menu" href="../../announce/release-1.2.1.html">1.2.1 ANN</a></div></div>
14
<div><a class="menu" href="../../install.html">INSTALL</a></div></div>
16
<div><a class="menu" href="../../contact.html">CONTACT</a></div></div>
18
<div><a class="menu" href="../../changelog.html">CHANGELOG</a></div></div>
20
<div><a class="menu" href="../../faq.html">FAQ</a></div></div>
24
<div><a class="menu" href="../index.html">Index</a></div>
25
<div><a class="menu" href="../quickstart.html">Quickstart</a></div>
26
<div><a class="menu" href="../features.html">Features</a></div>
27
<div><a class="menu" href="index.html">Plugins</a></div>
28
<div><a class="menu" href="../funcargs.html">Funcargs</a></div>
29
<div><a class="menu" href="../customize.html">Customize</a></div>
30
<div><a class="menu" href="../talks.html">Tutorials</a></div></div></div>
33
<h3>supporting APIs:</h3>
34
<div><a class="menu" href="../../index.html">Index</a></div>
35
<div><a class="menu" href="../../path.html">py.path</a></div>
36
<div><a class="menu" href="../../code.html">py.code</a></div></div></div></div></div>
37
<div id="contentspace">
40
<div class="section" id="configurable-per-test-stdout-stderr-capturing-mechanisms">
41
<h1>configurable per-test stdout/stderr capturing mechanisms.</h1>
42
<div class="contents local topic" id="contents">
44
<li><a class="reference internal" href="#capturing-of-input-output-streams-during-tests" id="id1">Capturing of input/output streams during tests</a></li>
45
<li><a class="reference internal" href="#sys-level-capturing" id="id2">sys-level capturing</a></li>
46
<li><a class="reference internal" href="#fd-level-capturing-and-subprocesses" id="id3">FD-level capturing and subprocesses</a></li>
47
<li><a class="reference internal" href="#example-usage-of-the-capturing-function-arguments" id="id4">Example Usage of the capturing Function arguments</a></li>
48
<li><a class="reference internal" href="#the-capsys-test-function-argument" id="id5">the 'capsys' test function argument</a></li>
49
<li><a class="reference internal" href="#the-capfd-test-function-argument" id="id6">the 'capfd' test function argument</a></li>
50
<li><a class="reference internal" href="#command-line-options" id="id7">command line options</a></li>
53
<p>This plugin captures stdout/stderr output for each test separately.
54
In case of test failures this captured output is shown grouped
55
togtther with the test.</p>
56
<p>The plugin also provides test function arguments that help to
57
assert stdout/stderr output from within your tests, see the
58
<a class="reference internal" href="#funcarg-example">funcarg example</a>.</p>
59
<div class="section" id="capturing-of-input-output-streams-during-tests">
60
<h2><a class="toc-backref" href="#id1">Capturing of input/output streams during tests</a></h2>
61
<p>By default <tt class="docutils literal">sys.stdout</tt> and <tt class="docutils literal">sys.stderr</tt> are substituted with
62
temporary streams during the execution of tests and setup/teardown code.
63
During the whole testing process it will re-use the same temporary
64
streams allowing to play well with the logging module which easily
65
takes ownership on these streams.</p>
66
<p>Also, 'sys.stdin' is substituted with a file-like "null" object that
67
does not return any values. This is to immediately error out
68
on tests that wait on reading something from stdin.</p>
69
<p>You can influence output capturing mechanisms from the command line:</p>
70
<pre class="literal-block">
71
py.test -s # disable all capturing
72
py.test --capture=sys # replace sys.stdout/stderr with in-mem files
73
py.test --capture=fd # point filedescriptors 1 and 2 to temp file
75
<p>If you set capturing values in a conftest file like this:</p>
76
<pre class="literal-block">
80
<p>then all tests in that directory will execute with "fd" style capturing.</p>
82
<div class="section" id="sys-level-capturing">
83
<h2><a class="toc-backref" href="#id2">sys-level capturing</a></h2>
84
<p>Capturing on 'sys' level means that <tt class="docutils literal">sys.stdout</tt> and <tt class="docutils literal">sys.stderr</tt>
85
will be replaced with in-memory files (<tt class="docutils literal">py.io.TextIO</tt> to be precise)
86
that capture writes and decode non-unicode strings to a unicode object
87
(using a default, usually, UTF-8, encoding).</p>
89
<div class="section" id="fd-level-capturing-and-subprocesses">
90
<h2><a class="toc-backref" href="#id3">FD-level capturing and subprocesses</a></h2>
91
<p>The <tt class="docutils literal">fd</tt> based method means that writes going to system level files
92
based on the standard file descriptors will be captured, for example
93
writes such as <tt class="docutils literal">os.write(1, 'hello')</tt> will be captured properly.
94
Capturing on fd-level will include output generated from
95
any subprocesses created during a test.</p>
97
<div class="section" id="example-usage-of-the-capturing-function-arguments">
98
<span id="funcarg-example"></span><h2><a class="toc-backref" href="#id4">Example Usage of the capturing Function arguments</a></h2>
99
<p>You can use the <a class="reference internal" href="#capsys-funcarg">capsys funcarg</a> and <a class="reference internal" href="#capfd-funcarg">capfd funcarg</a> to
100
capture writes to stdout and stderr streams. Using the
101
funcargs frees your test from having to care about setting/resetting
102
the old streams and also interacts well with py.test's own
103
per-test capturing. Here is an example test function:</p>
104
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">test_myoutput</span>(capsys):
105
<span style="color: #008000; font-weight: bold">print</span> (<span style="color: #BA2121">"hello"</span>)
106
sys<span style="color: #666666">.</span>stderr<span style="color: #666666">.</span>write(<span style="color: #BA2121">"world</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">"</span>)
107
out, err <span style="color: #666666">=</span> capsys<span style="color: #666666">.</span>readouterr()
108
<span style="color: #008000; font-weight: bold">assert</span> out <span style="color: #666666">==</span> <span style="color: #BA2121">"hello</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">"</span>
109
<span style="color: #008000; font-weight: bold">assert</span> err <span style="color: #666666">==</span> <span style="color: #BA2121">"world</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">"</span>
110
<span style="color: #008000; font-weight: bold">print</span> <span style="color: #BA2121">"next"</span>
111
out, err <span style="color: #666666">=</span> capsys<span style="color: #666666">.</span>readouterr()
112
<span style="color: #008000; font-weight: bold">assert</span> out <span style="color: #666666">==</span> <span style="color: #BA2121">"next</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">"</span>
114
<p>The <tt class="docutils literal">readouterr()</tt> call snapshots the output so far -
115
and capturing will be continued. After the test
116
function finishes the original streams will
117
be restored. If you want to capture on
118
the filedescriptor level you can use the <tt class="docutils literal">capfd</tt> function
119
argument which offers the same interface.</p>
121
<div class="section" id="the-capsys-test-function-argument">
122
<span id="capsys-funcarg"></span><h2><a class="toc-backref" href="#id5">the 'capsys' test function argument</a></h2>
123
<p>captures writes to sys.stdout/sys.stderr and makes
124
them available successively via a <tt class="docutils literal">capsys.readouterr()</tt> method
125
which returns a <tt class="docutils literal">(out, err)</tt> tuple of captured snapshot strings.</p>
127
<div class="section" id="the-capfd-test-function-argument">
128
<span id="capfd-funcarg"></span><h2><a class="toc-backref" href="#id6">the 'capfd' test function argument</a></h2>
129
<p>captures writes to file descriptors 1 and 2 and makes
130
snapshotted <tt class="docutils literal">(out, err)</tt> string tuples available
131
via the <tt class="docutils literal">capsys.readouterr()</tt> method. If the underlying
132
platform does not have <tt class="docutils literal">os.dup</tt> (e.g. Jython) tests using
133
this funcarg will automatically skip.</p>
135
<div class="section" id="command-line-options">
136
<h2><a class="toc-backref" href="#id7">command line options</a></h2>
137
<dl class="docutils">
138
<dt><tt class="docutils literal"><span class="pre">--capture=method</span></tt></dt>
139
<dd>per-test capturing method: one of fd (default)|sys|no.</dd>
140
<dt><tt class="docutils literal"><span class="pre">-s</span></tt></dt>
141
<dd>shortcut for --capture=no.</dd>
145
<div class="section" id="start-improving-this-plugin-in-30-seconds">
146
<h1>Start improving this plugin in 30 seconds</h1>
147
<ol class="arabic simple">
148
<li>Download <a class="reference external" href="http://bitbucket.org/hpk42/py-trunk/raw/1.2.0/py/_plugin/pytest_capture.py">pytest_capture.py</a> plugin source code</li>
149
<li>put it somewhere as <tt class="docutils literal">pytest_capture.py</tt> into your import path</li>
150
<li>a subsequent <tt class="docutils literal">py.test</tt> run will use your local version</li>
152
<p>Checkout <a class="reference external" href="../customize.html">customize</a>, other <a class="reference external" href="index.html">plugins</a> or <a class="reference external" href="../../contact.html">get in contact</a>.</p>
155
<script type="text/javascript">
156
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
157
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
159
<script type="text/javascript">
161
var pageTracker = _gat._getTracker("UA-7597274-3");
162
pageTracker._trackPageview();
163
} catch(err) {}</script>
b'\\ No newline at end of file'