<!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>Django and doctests — Django 1.5.8 documentation</title> <link rel="stylesheet" href="../../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../../', VERSION: '1.5.8', 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="Django 1.5.8 documentation" href="../../index.html" /> <link rel="up" title="Testing in Django" href="index.html" /> <link rel="next" title="Advanced testing topics" href="advanced.html" /> <link rel="prev" title="Testing Django applications" href="overview.html" /> <script type="text/javascript" src="../../templatebuiltins.js"></script> <script type="text/javascript"> (function($) { if (!django_template_builtins) { // templatebuiltins.js missing, do nothing. return; } $(document).ready(function() { // Hyperlink Django template tags and filters var base = "../../ref/templates/builtins.html"; if (base == "#") { // Special case for builtins.html itself base = ""; } // Tags are keywords, class '.k' $("div.highlight\\-html\\+django span.k").each(function(i, elem) { var tagname = $(elem).text(); if ($.inArray(tagname, django_template_builtins.ttags) != -1) { var fragment = tagname.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>"); } }); // Filters are functions, class '.nf' $("div.highlight\\-html\\+django span.nf").each(function(i, elem) { var filtername = $(elem).text(); if ($.inArray(filtername, django_template_builtins.tfilters) != -1) { var fragment = filtername.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>"); } }); }); })(jQuery); </script> </head> <body> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../../index.html">Django 1.5.8 documentation</a></h1> <div id="global-nav"> <a title="Home page" href="../../index.html">Home</a> | <a title="Table of contents" href="../../contents.html">Table of contents</a> | <a title="Global index" href="../../genindex.html">Index</a> | <a title="Module index" href="../../py-modindex.html">Modules</a> </div> <div class="nav"> « <a href="overview.html" title="Testing Django applications">previous</a> | <a href="../index.html" title="Using Django" accesskey="U">up</a> | <a href="advanced.html" title="Advanced testing topics">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="topics-testing-doctests"> <div class="section" id="s-django-and-doctests"> <span id="django-and-doctests"></span><h1>Django and doctests<a class="headerlink" href="#django-and-doctests" title="Permalink to this headline">ΒΆ</a></h1> <p>Doctests use Python’s standard <a class="reference external" href="http://docs.python.org/2.7/library/doctest.html#doctest" title="(in Python v2.7)"><tt class="xref py py-mod docutils literal"><span class="pre">doctest</span></tt></a> module, which searches your docstrings for statements that resemble a session of the Python interactive interpreter. A full explanation of how <a class="reference external" href="http://docs.python.org/2.7/library/doctest.html#doctest" title="(in Python v2.7)"><tt class="xref py py-mod docutils literal"><span class="pre">doctest</span></tt></a> works is out of the scope of this document; read Python’s official documentation for the details.</p> <div class="admonition-what-s-a-docstring admonition"> <p class="first admonition-title">What’s a <strong>docstring</strong>?</p> <p>A good explanation of docstrings (and some guidelines for using them effectively) can be found in <span class="target" id="index-0"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0257"><strong>PEP 257</strong></a>:</p> <blockquote> <div>A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the <tt class="docutils literal"><span class="pre">__doc__</span></tt> special attribute of that object.</div></blockquote> <p>For example, this function has a docstring that describes what it does:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">add_two</span><span class="p">(</span><span class="n">num</span><span class="p">):</span> <span class="s">"Return the result of adding two to the provided number."</span> <span class="k">return</span> <span class="n">num</span> <span class="o">+</span> <span class="mi">2</span> </pre></div> </div> <p class="last">Because tests often make great documentation, putting tests directly in your docstrings is an effective way to document <em>and</em> test your code.</p> </div> <p>As with unit tests, for a given Django application, the test runner looks for doctests in two places:</p> <ul class="simple"> <li>The <tt class="docutils literal"><span class="pre">models.py</span></tt> file. You can define module-level doctests and/or a doctest for individual models. It’s common practice to put application-level doctests in the module docstring and model-level doctests in the model docstrings.</li> <li>A file called <tt class="docutils literal"><span class="pre">tests.py</span></tt> in the application directory – i.e., the directory that holds <tt class="docutils literal"><span class="pre">models.py</span></tt>. This file is a hook for any and all doctests you want to write that aren’t necessarily related to models.</li> </ul> <p>This example doctest is equivalent to the example given in the unittest section above:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># models.py</span> <span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span> <span class="k">class</span> <span class="nc">Animal</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> An animal that knows how to make noise</span> <span class="sd"> # Create some animals</span> <span class="sd"> >>> lion = Animal.objects.create(name="lion", sound="roar")</span> <span class="sd"> >>> cat = Animal.objects.create(name="cat", sound="meow")</span> <span class="sd"> # Make 'em speak</span> <span class="sd"> >>> lion.speak()</span> <span class="sd"> 'The lion says "roar"'</span> <span class="sd"> >>> cat.speak()</span> <span class="sd"> 'The cat says "meow"'</span> <span class="sd"> """</span> <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span> <span class="n">sound</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span> <span class="k">def</span> <span class="nf">speak</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="s">'The </span><span class="si">%s</span><span class="s"> says "</span><span class="si">%s</span><span class="s">"'</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sound</span><span class="p">)</span> </pre></div> </div> <p>When you <a class="reference internal" href="overview.html#running-tests"><em>run your tests</em></a>, the test runner will find this docstring, notice that portions of it look like an interactive Python session, and execute those lines while checking that the results match.</p> <p>In the case of model tests, note that the test runner takes care of creating its own test database. That is, any test that accesses a database – by creating and saving model instances, for example – will not affect your production database. However, the database is not refreshed between doctests, so if your doctest requires a certain state you should consider flushing the database or loading a fixture. (See the section on <a class="reference internal" href="overview.html#topics-testing-fixtures"><em>fixtures</em></a> for more on this.) Note that to use this feature, the database user Django is connecting as must have <tt class="docutils literal"><span class="pre">CREATE</span> <span class="pre">DATABASE</span></tt> rights.</p> <p>For more details about <a class="reference external" href="http://docs.python.org/2.7/library/doctest.html#doctest" title="(in Python v2.7)"><tt class="xref py py-mod docutils literal"><span class="pre">doctest</span></tt></a>, see the Python documentation.</p> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3>Browse</h3> <ul> <li>Prev: <a href="overview.html">Testing Django applications</a></li> <li>Next: <a href="advanced.html">Advanced testing topics</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../../index.html">Django 1.5.8 documentation</a> <ul><li><a href="../index.html">Using Django</a> <ul><li><a href="index.html">Testing in Django</a> <ul><li>Django and doctests</li></ul> </li></ul></li></ul> </li> </ul> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/topics/testing/doctests.txt" rel="nofollow">Show Source</a></li> </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> <h3>Last update:</h3> <p class="topless">May 15, 2014</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="overview.html" title="Testing Django applications">previous</a> | <a href="../index.html" title="Using Django" accesskey="U">up</a> | <a href="advanced.html" title="Advanced testing topics">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>