<!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/sphinxdoc.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.3.4.1', 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="top" title="None" href="index.html" /> <link rel="up" title="py.test 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" /> </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.3.4.1</a> »</li> <li><a href="apiref.html" accesskey="U">py.test reference documentation</a> »</li> <g:plusone></g:plusone> </ul> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <div id="searchbox" style="display: none"> <form class="search" action="search.html" method="get"> <input type="text" name="q" size="18" /> <input type="submit" value="Search" /> <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> <h3>quicklinks</h3> <div style="text-align: left; font-size: 100%; vertical-align: middle;"> <table> <tr> <td> <a href="index.html">home</a> </td><td> <a href="contents.html">TOC/contents</a> </td></tr><tr><td> <a href="getting-started.html">install</a> </td><td> <a href="changelog.html">changelog</a> </td></tr><tr><td> <a href="example/index.html">examples</a> </td><td> <a href="customize.html">customize</a> </td></tr><tr><td> <a href="https://bitbucket.org/hpk42/pytest/issues?status=new&status=open">issues[bb]</a> </td><td> <a href="contact.html">contact</a> </td></tr></table> </div> <h3><a href="contents.html">Table Of Contents</a></h3> <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> <h4>Previous topic</h4> <p class="topless"><a href="skipping.html" title="previous chapter">Skip and xfail: dealing with tests that can not succeed</a></p> <h4>Next topic</h4> <p class="topless"><a href="unittest.html" title="next chapter">Support for unittest.TestCase / Integration of fixtures</a></p> </div> </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"> <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"> <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="clearer"></div> </div> <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" >next</a></li> <li class="right" > <a href="skipping.html" title="Skip and xfail: dealing with tests that can not succeed" >previous</a> |</li> <li><a href="contents.html">pytest-2.3.4.1</a> »</li> <li><a href="apiref.html" >py.test reference documentation</a> »</li> <g:plusone></g:plusone> </ul> </div> <div class="footer"> © Copyright 2012, holger krekel. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </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> <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> </body> </html>