Current File : //usr/share/doc/pytest-2.7.0/html/en/recwarn.html |
<!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 deprecation and other 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.7.0',
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="Support for unittest.TestCase / Integration of fixtures" href="unittest.html" />
<link rel="prev" title="Skip and xfail: dealing with tests that can not succeed" href="skipping.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="unittest.html" title="Support for unittest.TestCase / Integration of fixtures"
accesskey="N">next</a></li>
<li class="right" >
<a href="skipping.html" title="Skip and xfail: dealing with tests that can not succeed"
accesskey="P">previous</a> |</li>
<li><a href="contents.html">pytest-2.7.0</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="asserting-deprecation-and-other-warnings">
<h1>Asserting deprecation and other warnings<a class="headerlink" href="#asserting-deprecation-and-other-warnings" title="Permalink to this headline">¶</a></h1>
<div class="section" id="the-recwarn-function-argument">
<span id="function-argument"></span><h2>The recwarn function argument<a class="headerlink" href="#the-recwarn-function-argument" title="Permalink to this headline">¶</a></h2>
<p>You can use the <tt class="docutils literal"><span class="pre">recwarn</span></tt> funcarg to assert that code triggers
warnings through the Python warnings system. Here is a simple
self-contained test:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># content of test_recwarn.py</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="kn">from</span> <span class="nn">warnings</span> <span class="kn">import</span> <span class="n">warn</span>
<span class="n">warn</span><span class="p">(</span><span class="s">"hello"</span><span class="p">,</span> <span class="ne">DeprecationWarning</span><span class="p">)</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">DeprecationWarning</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">DeprecationWarning</span><span class="p">)</span>
<span class="k">assert</span> <span class="s">'hello'</span> <span class="ow">in</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="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>The <tt class="docutils literal"><span class="pre">recwarn</span></tt> function argument provides these methods:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">pop(category=None)</span></tt>: return last warning matching the category.</li>
<li><tt class="docutils literal"><span class="pre">clear()</span></tt>: clear list of warnings</li>
</ul>
</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 Deprecation
warning:</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>
</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="#">Asserting deprecation and other warnings</a><ul>
<li><a class="reference internal" href="#the-recwarn-function-argument">The recwarn function argument</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>
<li><a href="apiref.html">pytest reference documentation</a><ul>
<li>Previous: <a href="skipping.html" title="previous chapter">Skip and xfail: dealing with tests that can not succeed</a></li>
<li>Next: <a href="unittest.html" title="next chapter">Support for unittest.TestCase / Integration of fixtures</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/pytest-dev/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/pytest-dev/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>
<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>
</body>
</html>