<!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>非推奨の警告やその他の警告のアサート</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.2.4.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> <script type="text/javascript" src="_static/translations.js"></script> <link rel="top" title="None" href="index.html" /> <link rel="up" title="py.test リファレンスドキュメント" href="apiref.html" /> <link rel="next" title="unittest.TestCase の対応" href="unittest.html" /> <link rel="prev" title="属性をもつテスト関数のマーク" href="mark.html" /> </head> <body> <div class="related"> <h3>ナビゲーション</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="py-modindex.html" title="Pythonモジュール索引" >モジュール</a></li> <li class="right" > <a href="unittest.html" title="unittest.TestCase の対応" accesskey="N">次へ</a> |</li> <li class="right" > <a href="mark.html" title="属性をもつテスト関数のマーク" accesskey="P">前へ</a> |</li> <li><a href="contents.html">pytest-2.2.4.0</a> »</li> <li><a href="apiref.html" accesskey="U">py.test リファレンスドキュメント</a> »</li> </ul> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="contents.html">目次</a></h3> <ul> <li><a class="reference internal" href="#">非推奨の警告やその他の警告のアサート</a><ul> <li><a class="reference internal" href="#recwarn">関数の引数 recwarn</a></li> <li><a class="reference internal" href="#id2">非推奨の警告を発生させる関数の確認</a></li> </ul> </li> </ul> <h4>前のトピックへ</h4> <p class="topless"><a href="mark.html" title="前の章へ">属性をもつテスト関数のマーク</a></p> <h4>次のトピックへ</h4> <p class="topless"><a href="unittest.html" title="次の章へ">unittest.TestCase の対応</a></p> <div id="searchbox" style="display: none"> <h3>クイック検索</h3> <form class="search" action="search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="検索" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> モジュール、クラス、または関数名を入力してください </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="id1"> <h1>非推奨の警告やその他の警告のアサート<a class="headerlink" href="#id1" title="このヘッドラインへのパーマリンク">¶</a></h1> <div class="section" id="recwarn"> <h2>関数の引数 recwarn<a class="headerlink" href="#recwarn" title="このヘッドラインへのパーマリンク">¶</a></h2> <p>Python のワーニングシステムからの警告を受け取るコードをアサートするために <tt class="docutils literal"><span class="pre">recwarn</span></tt> という関数の引数が使えます。簡単な自己完結型のテストを紹介します:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># 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>関数の引数 <tt class="docutils literal"><span class="pre">recwarn</span></tt> は次のメソッドを提供します:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">pop(category=None)</span></tt>: カテゴリに一致する最後の警告を返す</li> <li><tt class="docutils literal"><span class="pre">clear()</span></tt>: 警告の一覧をクリアする</li> </ul> </div> <div class="section" id="id2"> <h2>非推奨の警告を発生させる関数の確認<a class="headerlink" href="#id2" title="このヘッドラインへのパーマリンク">¶</a></h2> <p>非推奨の警告を発生させる特定の関数呼び出しを確認するためのグローバルなヘルパー関数も呼び出せます:</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>ナビゲーション</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="py-modindex.html" title="Pythonモジュール索引" >モジュール</a></li> <li class="right" > <a href="unittest.html" title="unittest.TestCase の対応" >次へ</a> |</li> <li class="right" > <a href="mark.html" title="属性をもつテスト関数のマーク" >前へ</a> |</li> <li><a href="contents.html">pytest-2.2.4.0</a> »</li> <li><a href="apiref.html" >py.test リファレンスドキュメント</a> »</li> </ul> </div> <div class="footer"> © Copyright 2011, holger krekel et alii. このドキュメントは <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3 で生成しました。 </div> </body> </html>