/usr/share/doc/python-pytest-doc/html/recwarn.html is in python-pytest-doc 2.8.7-4.
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 | <!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>Asserting Warnings</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.8.7',
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="contents.html" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9">
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="nav-item nav-item-0"><a href="contents.html">pytest-2.8.7</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="asserting-warnings">
<h1>Asserting Warnings<a class="headerlink" href="#asserting-warnings" title="Permalink to this headline">¶</a></h1>
<div class="section" id="asserting-warnings-with-the-warns-function">
<span id="warns"></span><h2>Asserting warnings with the warns function<a class="headerlink" href="#asserting-warnings-with-the-warns-function" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.8.</span></p>
</div>
<p>You can check that code raises a particular warning using <code class="docutils literal"><span class="pre">pytest.warns</span></code>,
which works in a similar manner to <a class="reference internal" href="assert.html#assertraises"><span>raises</span></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">import</span> <span class="nn">pytest</span>
<span class="k">def</span> <span class="nf">test_warning</span><span class="p">():</span>
<span class="k">with</span> <span class="n">pytest</span><span class="o">.</span><span class="n">warns</span><span class="p">(</span><span class="ne">UserWarning</span><span class="p">):</span>
<span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">"my warning"</span><span class="p">,</span> <span class="ne">UserWarning</span><span class="p">)</span>
</pre></div>
</div>
<p>The test will fail if the warning in question is not raised.</p>
<p>You can also call <code class="docutils literal"><span class="pre">pytest.warns</span></code> on a function or code string:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">pytest</span><span class="o">.</span><span class="n">warns</span><span class="p">(</span><span class="n">expected_warning</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">pytest</span><span class="o">.</span><span class="n">warns</span><span class="p">(</span><span class="n">expected_warning</span><span class="p">,</span> <span class="s2">"func(*args, **kwargs)"</span><span class="p">)</span>
</pre></div>
</div>
<p>The function also returns a list of all raised warnings (as
<code class="docutils literal"><span class="pre">warnings.WarningMessage</span></code> objects), which you can query for
additional information:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">with</span> <span class="n">pytest</span><span class="o">.</span><span class="n">warns</span><span class="p">(</span><span class="ne">RuntimeWarning</span><span class="p">)</span> <span class="k">as</span> <span class="n">record</span><span class="p">:</span>
<span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">"another warning"</span><span class="p">,</span> <span class="ne">RuntimeWarning</span><span class="p">)</span>
<span class="c1"># check that only one warning was raised</span>
<span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">record</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span>
<span class="c1"># check that the message matches</span>
<span class="k">assert</span> <span class="n">record</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">message</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s2">"another warning"</span>
</pre></div>
</div>
<p>Alternatively, you can examine raised warnings in detail using the
<a class="reference internal" href="#recwarn"><span>recwarn</span></a> fixture (see below).</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><code class="docutils literal"><span class="pre">DeprecationWarning</span></code> and <code class="docutils literal"><span class="pre">PendingDeprecationWarning</span></code> are treated
differently; see <a class="reference internal" href="#ensuring-function-triggers"><span>Ensuring a function triggers a deprecation warning</span></a>.</p>
</div>
</div>
<div class="section" id="recording-warnings">
<span id="recwarn"></span><h2>Recording warnings<a class="headerlink" href="#recording-warnings" title="Permalink to this headline">¶</a></h2>
<p>You can record raised warnings either using <code class="docutils literal"><span class="pre">pytest.warns</span></code> or with
the <code class="docutils literal"><span class="pre">recwarn</span></code> fixture.</p>
<p>To record with <code class="docutils literal"><span class="pre">pytest.warns</span></code> without asserting anything about the warnings,
pass <code class="docutils literal"><span class="pre">None</span></code> as the expected warning type:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">with</span> <span class="n">pytest</span><span class="o">.</span><span class="n">warns</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span> <span class="k">as</span> <span class="n">record</span><span class="p">:</span>
<span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">"user"</span><span class="p">,</span> <span class="ne">UserWarning</span><span class="p">)</span>
<span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">"runtime"</span><span class="p">,</span> <span class="ne">RuntimeWarning</span><span class="p">)</span>
<span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">record</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span>
<span class="k">assert</span> <span class="nb">str</span><span class="p">(</span><span class="n">record</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">message</span><span class="p">)</span> <span class="o">==</span> <span class="s2">"user"</span>
<span class="k">assert</span> <span class="nb">str</span><span class="p">(</span><span class="n">record</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">message</span><span class="p">)</span> <span class="o">==</span> <span class="s2">"runtime"</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">recwarn</span></code> fixture will record warnings for the whole function:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">warnings</span>
<span class="k">def</span> <span class="nf">test_hello</span><span class="p">(</span><span class="n">recwarn</span><span class="p">):</span>
<span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">"hello"</span><span class="p">,</span> <span class="ne">UserWarning</span><span class="p">)</span>
<span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">recwarn</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span>
<span class="n">w</span> <span class="o">=</span> <span class="n">recwarn</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="ne">UserWarning</span><span class="p">)</span>
<span class="k">assert</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">w</span><span class="o">.</span><span class="n">category</span><span class="p">,</span> <span class="ne">UserWarning</span><span class="p">)</span>
<span class="k">assert</span> <span class="nb">str</span><span class="p">(</span><span class="n">w</span><span class="o">.</span><span class="n">message</span><span class="p">)</span> <span class="o">==</span> <span class="s2">"hello"</span>
<span class="k">assert</span> <span class="n">w</span><span class="o">.</span><span class="n">filename</span>
<span class="k">assert</span> <span class="n">w</span><span class="o">.</span><span class="n">lineno</span>
</pre></div>
</div>
<p>Both <code class="docutils literal"><span class="pre">recwarn</span></code> and <code class="docutils literal"><span class="pre">pytest.warns</span></code> return the same interface for recorded
warnings: a WarningsRecorder instance. To view the recorded warnings, you can
iterate over this instance, call <code class="docutils literal"><span class="pre">len</span></code> on it to get the number of recorded
warnings, or index into it to get a particular recorded warning. It also
provides these methods:</p>
<dl class="class">
<dt id="_pytest.recwarn.WarningsRecorder">
<em class="property">class </em><code class="descname">WarningsRecorder</code><a class="reference internal" href="_modules/_pytest/recwarn.html#WarningsRecorder"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.recwarn.WarningsRecorder" title="Permalink to this definition">¶</a></dt>
<dd><p>A context manager to record raised warnings.</p>
<p>Adapted from <cite>warnings.catch_warnings</cite>.</p>
<dl class="attribute">
<dt id="_pytest.recwarn.WarningsRecorder.list">
<code class="descname">list</code><a class="headerlink" href="#_pytest.recwarn.WarningsRecorder.list" title="Permalink to this definition">¶</a></dt>
<dd><p>The list of recorded warnings.</p>
</dd></dl>
<dl class="method">
<dt id="_pytest.recwarn.WarningsRecorder.pop">
<code class="descname">pop</code><span class="sig-paren">(</span><em>cls=<type 'exceptions.Warning'></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/recwarn.html#WarningsRecorder.pop"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.recwarn.WarningsRecorder.pop" title="Permalink to this definition">¶</a></dt>
<dd><p>Pop the first recorded warning, raise exception if not exists.</p>
</dd></dl>
<dl class="method">
<dt id="_pytest.recwarn.WarningsRecorder.clear">
<code class="descname">clear</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/recwarn.html#WarningsRecorder.clear"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.recwarn.WarningsRecorder.clear" title="Permalink to this definition">¶</a></dt>
<dd><p>Clear the list of recorded warnings.</p>
</dd></dl>
</dd></dl>
<p>Each recorded warning has the attributes <code class="docutils literal"><span class="pre">message</span></code>, <code class="docutils literal"><span class="pre">category</span></code>,
<code class="docutils literal"><span class="pre">filename</span></code>, <code class="docutils literal"><span class="pre">lineno</span></code>, <code class="docutils literal"><span class="pre">file</span></code>, and <code class="docutils literal"><span class="pre">line</span></code>. The <code class="docutils literal"><span class="pre">category</span></code> is the
class of the warning. The <code class="docutils literal"><span class="pre">message</span></code> is the warning itself; calling
<code class="docutils literal"><span class="pre">str(message)</span></code> will return the actual message of the warning.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><code class="docutils literal"><span class="pre">DeprecationWarning</span></code> and <code class="docutils literal"><span class="pre">PendingDeprecationWarning</span></code> are treated
differently; see <a class="reference internal" href="#ensuring-function-triggers"><span>Ensuring a function triggers a deprecation warning</span></a>.</p>
</div>
</div>
<div class="section" id="ensuring-a-function-triggers-a-deprecation-warning">
<span id="ensuring-function-triggers"></span><h2>Ensuring a function triggers a deprecation warning<a class="headerlink" href="#ensuring-a-function-triggers-a-deprecation-warning" title="Permalink to this headline">¶</a></h2>
<p>You can also call a global helper for checking
that a certain function call triggers a <code class="docutils literal"><span class="pre">DeprecationWarning</span></code> or
<code class="docutils literal"><span class="pre">PendingDeprecationWarning</span></code>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pytest</span>
<span class="k">def</span> <span class="nf">test_global</span><span class="p">():</span>
<span class="n">pytest</span><span class="o">.</span><span class="n">deprecated_call</span><span class="p">(</span><span class="n">myfunction</span><span class="p">,</span> <span class="mi">17</span><span class="p">)</span>
</pre></div>
</div>
<p>By default, <code class="docutils literal"><span class="pre">DeprecationWarning</span></code> and <code class="docutils literal"><span class="pre">PendingDeprecationWarning</span></code> will not be
caught when using <code class="docutils literal"><span class="pre">pytest.warns</span></code> or <code class="docutils literal"><span class="pre">recwarn</span></code> because default Python warnings filters hide
them. If you wish to record them in your own code, use the
command <code class="docutils literal"><span class="pre">warnings.simplefilter('always')</span></code>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">import</span> <span class="nn">pytest</span>
<span class="k">def</span> <span class="nf">test_deprecation</span><span class="p">(</span><span class="n">recwarn</span><span class="p">):</span>
<span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s1">'always'</span><span class="p">)</span>
<span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">"deprecated"</span><span class="p">,</span> <span class="ne">DeprecationWarning</span><span class="p">)</span>
<span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">recwarn</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span>
<span class="k">assert</span> <span class="n">recwarn</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="ne">DeprecationWarning</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>
</ul>
<hr>
<ul>
<li><a class="reference internal" href="#">Asserting Warnings</a><ul>
<li><a class="reference internal" href="#asserting-warnings-with-the-warns-function">Asserting warnings with the warns function</a></li>
<li><a class="reference internal" href="#recording-warnings">Recording warnings</a></li>
<li><a class="reference internal" href="#ensuring-a-function-triggers-a-deprecation-warning">Ensuring a function triggers a deprecation warning</a></li>
</ul>
</li>
</ul>
<h3>Related Topics</h3>
<ul>
<li><a href="contents.html">Documentation overview</a><ul>
</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="http://pytest.org/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">
<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 2015, holger krekel and pytest-dev team.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
</div>
</body>
</html>
|