<!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" lang=""> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Testing in Django — Django 1.11.20 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" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></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/language_data.js"></script> <link rel="index" title="Index" href="../../genindex.html" /> <link rel="search" title="Search" href="../../search.html" /> <link rel="next" title="Writing and running tests" href="overview.html" /> <link rel="prev" title="Managing files" href="../files.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.11.20 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="../files.html" title="Managing files">previous</a> | <a href="../index.html" title="Using Django" accesskey="U">up</a> | <a href="overview.html" title="Writing and running tests">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="topics-testing-index"> <div class="section" id="s-testing-in-django"> <span id="testing-in-django"></span><h1>Testing in Django<a class="headerlink" href="#testing-in-django" title="Permalink to this headline">¶</a></h1> <p>Automated testing is an extremely useful bug-killing tool for the modern Web developer. You can use a collection of tests – a <strong>test suite</strong> – to solve, or avoid, a number of problems:</p> <ul class="simple"> <li>When you’re writing new code, you can use tests to validate your code works as expected.</li> <li>When you’re refactoring or modifying old code, you can use tests to ensure your changes haven’t affected your application’s behavior unexpectedly.</li> </ul> <p>Testing a Web application is a complex task, because a Web application is made of several layers of logic – from HTTP-level request handling, to form validation and processing, to template rendering. With Django’s test-execution framework and assorted utilities, you can simulate requests, insert test data, inspect your application’s output and generally verify your code is doing what it should be doing.</p> <p>The best part is, it’s really easy.</p> <p>The preferred way to write tests in Django is using the <code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code> module built in to the Python standard library. This is covered in detail in the <a class="reference internal" href="overview.html"><span class="doc">Writing and running tests</span></a> document.</p> <p>You can also use any <em>other</em> Python test framework; Django provides an API and tools for that kind of integration. They are described in the <a class="reference internal" href="advanced.html#other-testing-frameworks"><span class="std std-ref">Using different testing frameworks</span></a> section of <a class="reference internal" href="advanced.html"><span class="doc">Advanced testing topics</span></a>.</p> <div class="toctree-wrapper compound"> <ul> <li class="toctree-l1"><a class="reference internal" href="overview.html">Writing and running tests</a></li> <li class="toctree-l1"><a class="reference internal" href="tools.html">Testing tools</a></li> <li class="toctree-l1"><a class="reference internal" href="advanced.html">Advanced testing topics</a></li> </ul> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="../files.html" title="previous chapter">Managing files</a></p> <h4>Next topic</h4> <p class="topless"><a href="overview.html" title="next chapter">Writing and running tests</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/topics/testing/index.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <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> </div> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Feb 11, 2019</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="../files.html" title="Managing files">previous</a> | <a href="../index.html" title="Using Django" accesskey="U">up</a> | <a href="overview.html" title="Writing and running tests">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>