{"title":"Unit tests","version":"1.9","locale":"es","docname":"internals/contributing/writing-code/unit-tests","url":"/es/1.9/internals/contributing/writing-code/unit-tests/","canonical":"https://djangodocs.dev/es/1.9/internals/contributing/writing-code/unit-tests/","summary":"Django comes with a test suite of its own, in the tests directory of the code base. It’s our policy to make sure all tests pass at all times. We appreciate any and…","html":"<h1>Unit tests<a class=\"heading-anchor\" href=\"#unit-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Django comes with a test suite of its own, in the <code class=\"docutils literal notranslate\"><span class=\"pre\">tests</span></code> directory of the\ncode base. It’s our policy to make sure all tests pass at all times.</p>\n<p>We appreciate any and all contributions to the test suite!</p>\n<p>The Django tests all use the testing infrastructure that ships with Django for\ntesting applications. See <a class=\"reference internal\" href=\"/es/1.9/topics/testing/overview/\"><span class=\"doc\">Writing and running tests</span></a> for an explanation of\nhow to write new tests.</p>\n<section id=\"running-the-unit-tests\">\n<span id=\"running-unit-tests\"></span><h2>Running the unit tests<a class=\"heading-anchor\" href=\"#running-the-unit-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"quickstart\">\n<h3>Quickstart<a class=\"heading-anchor\" href=\"#quickstart\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you are on Python 2, you’ll first need to install a backport of the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">unittest.mock</span></code> module that’s available in Python 3. See\n<a class=\"reference internal\" href=\"#running-unit-tests-dependencies\"><span class=\"std std-ref\">Running all the tests</span></a> for details on installing <a class=\"reference external\" href=\"https://pypi.python.org/pypi/mock\">mock</a> and\nthe other optional test dependencies.</p>\n<p>Running the tests requires a Django settings module that defines the\ndatabases to use. To make it easy to get started, Django provides and uses a\nsample settings module that uses the SQLite database. To run the tests:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>git<span class=\"w\"> </span>clone<span class=\"w\"> </span>https://github.com/django/django.git<span class=\"w\"> </span>django-repo\n<span class=\"gp\">$ </span><span class=\"nb\">cd</span><span class=\"w\"> </span>django-repo/tests\n<span class=\"gp\">$ </span><span class=\"nv\">PYTHONPATH</span><span class=\"o\">=</span>..:<span class=\"nv\">$PYTHONPATH</span><span class=\"w\"> </span>./runtests.py\n</code></pre></div>\n<aside class=\"admonition-windows-users admonition\">\n<p class=\"admonition-title\">Windows users</p>\n<p>We recommend something like <a class=\"reference external\" href=\"https://msysgit.github.io/\">Git Bash</a> to run\nthe tests using the above approach.</p>\n</aside>\n<p>You can avoid typing the <code class=\"docutils literal notranslate\"><span class=\"pre\">PYTHONPATH</span></code> bit each time by adding your Django\ncheckout to your <code class=\"docutils literal notranslate\"><span class=\"pre\">PYTHONPATH</span></code> or by installing the source checkout using pip.\nSee <a class=\"reference internal\" href=\"/es/1.9/topics/install/#installing-development-version\"><span class=\"std std-ref\">Installing the development version</span></a>.</p>\n<p>Having problems? See <a class=\"reference internal\" href=\"#troubleshooting-unit-tests\"><span class=\"std std-ref\">Troubleshooting</span></a> for some common issues.</p>\n</section>\n<section id=\"using-another-settings-module\">\n<span id=\"running-unit-tests-settings\"></span><h3>Using another <code class=\"docutils literal notranslate\"><span class=\"pre\">settings</span></code> module<a class=\"heading-anchor\" href=\"#using-another-settings-module\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The included settings module allows you to run the test suite using\nSQLite. If you want to test behavior using a different database (and\nif you’re proposing patches for Django, it’s a good idea to test\nacross databases), you may need to define your own settings file.</p>\n<p>To run the tests with different settings, ensure that the module is on your\n<code class=\"docutils literal notranslate\"><span class=\"pre\">PYTHONPATH</span></code> and pass the module with <code class=\"docutils literal notranslate\"><span class=\"pre\">--settings</span></code>.</p>\n<p>The <a class=\"reference internal\" href=\"/es/1.9/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a> setting in any test settings module needs to define\ntwo databases:</p>\n<ul class=\"simple\">\n<li><p>A <code class=\"docutils literal notranslate\"><span class=\"pre\">default</span></code> database. This database should use the backend that\nyou want to use for primary testing.</p></li>\n<li><p>A database with the alias <code class=\"docutils literal notranslate\"><span class=\"pre\">other</span></code>. The <code class=\"docutils literal notranslate\"><span class=\"pre\">other</span></code> database is used to\nestablish that queries can be directed to different databases. As a result,\nthis database can use any backend you want. It doesn’t need to use the same\nbackend as the <code class=\"docutils literal notranslate\"><span class=\"pre\">default</span></code> database (although it can use the same backend if\nyou want to). It cannot be the same database as the <code class=\"docutils literal notranslate\"><span class=\"pre\">default</span></code>.</p></li>\n</ul>\n<p>If you’re using a backend that isn’t SQLite, you will need to provide other\ndetails for each database:</p>\n<ul class=\"simple\">\n<li><p>The <a class=\"reference internal\" href=\"/es/1.9/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USER</span></code></a> option needs to specify an existing user account\nfor the database. That user needs permission to execute <code class=\"docutils literal notranslate\"><span class=\"pre\">CREATE</span> <span class=\"pre\">DATABASE</span></code>\nso that the test database can be created.</p></li>\n<li><p>The <a class=\"reference internal\" href=\"/es/1.9/ref/settings/#std-setting-PASSWORD\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PASSWORD</span></code></a> option needs to provide the password for\nthe <a class=\"reference internal\" href=\"/es/1.9/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USER</span></code></a> that has been specified.</p></li>\n</ul>\n<p>Test databases get their names by prepending <code class=\"docutils literal notranslate\"><span class=\"pre\">test_</span></code> to the value of the\n<a class=\"reference internal\" href=\"/es/1.9/ref/settings/#std-setting-NAME\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">NAME</span></code></a> settings for the databases defined in <a class=\"reference internal\" href=\"/es/1.9/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a>.\nThese test databases are deleted when the tests are finished.</p>\n<p>You will also need to ensure that your database uses UTF-8 as the default\ncharacter set. If your database server doesn’t use UTF-8 as a default charset,\nyou will need to include a value for <a class=\"reference internal\" href=\"/es/1.9/ref/settings/#std-setting-TEST_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CHARSET</span></code></a> in the\ntest settings dictionary for the applicable database.</p>\n</section>\n<section id=\"running-only-some-of-the-tests\">\n<span id=\"runtests-specifying-labels\"></span><h3>Running only some of the tests<a class=\"heading-anchor\" href=\"#running-only-some-of-the-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django’s entire test suite takes a while to run, and running every single test\ncould be redundant if, say, you just added a test to Django that you want to\nrun quickly without running everything else. You can run a subset of the unit\ntests by appending the names of the test modules to <code class=\"docutils literal notranslate\"><span class=\"pre\">runtests.py</span></code> on the\ncommand line.</p>\n<p>For example, if you’d like to run tests only for generic relations and\ninternationalization, type:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>--settings<span class=\"o\">=</span>path.to.settings<span class=\"w\"> </span>generic_relations<span class=\"w\"> </span>i18n\n</code></pre></div>\n<p>How do you find out the names of individual tests? Look in <code class=\"docutils literal notranslate\"><span class=\"pre\">tests/</span></code> — each\ndirectory name there is the name of a test.</p>\n<p>If you just want to run a particular class of tests, you can specify a list of\npaths to individual test classes. For example, to run the <code class=\"docutils literal notranslate\"><span class=\"pre\">TranslationTests</span></code>\nof the <code class=\"docutils literal notranslate\"><span class=\"pre\">i18n</span></code> module, type:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>--settings<span class=\"o\">=</span>path.to.settings<span class=\"w\"> </span>i18n.tests.TranslationTests\n</code></pre></div>\n<p>Going beyond that, you can specify an individual test method like this:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>--settings<span class=\"o\">=</span>path.to.settings<span class=\"w\"> </span>i18n.tests.TranslationTests.test_lazy_objects\n</code></pre></div>\n</section>\n<section id=\"running-the-selenium-tests\">\n<h3>Running the Selenium tests<a class=\"heading-anchor\" href=\"#running-the-selenium-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Some tests require Selenium and a Web browser (Firefox, Google Chrome, or\nInternet Explorer). To allow those tests to be run rather than skipped, you must\ninstall the <a class=\"reference external\" href=\"https://pypi.python.org/pypi/selenium\">selenium</a> package into your Python path and run the tests with the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">--selenium</span></code> option:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>--settings<span class=\"o\">=</span>test_sqlite<span class=\"w\"> </span>--selenium<span class=\"w\"> </span>admin_inlines\n</code></pre></div>\n</section>\n<section id=\"running-all-the-tests\">\n<span id=\"running-unit-tests-dependencies\"></span><h3>Running all the tests<a class=\"heading-anchor\" href=\"#running-all-the-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you want to run the full suite of tests, you’ll need to install a number of\ndependencies:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference external\" href=\"https://pypi.python.org/pypi/bcrypt\">bcrypt</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.python.org/pypi/docutils\">docutils</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.python.org/pypi/geoip2\">geoip2</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.python.org/pypi/jinja2\">jinja2</a> 2.7+</p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.python.org/pypi/numpy\">numpy</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.python.org/pypi/Pillow/\">Pillow</a></p></li>\n<li><p><a class=\"reference external\" href=\"http://pyyaml.org/wiki/PyYAML\">PyYAML</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.python.org/pypi/pytz/\">pytz</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.python.org/pypi/setuptools/\">setuptools</a></p></li>\n<li><p><a class=\"reference external\" href=\"http://memcached.org/\">memcached</a>, plus a <a class=\"reference internal\" href=\"/es/1.9/topics/cache/#memcached\"><span class=\"std std-ref\">supported Python binding</span></a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.python.org/pypi/mock\">mock</a> (for Python 2)</p></li>\n<li><p><a class=\"reference external\" href=\"https://www.gnu.org/software/gettext/manual/gettext.html\">gettext</a> (<a class=\"reference internal\" href=\"/es/1.9/topics/i18n/translation/#gettext-on-windows\"><span class=\"std std-ref\">gettext on Windows</span></a>)</p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.python.org/pypi/selenium\">selenium</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.python.org/pypi/sqlparse\">sqlparse</a></p></li>\n</ul>\n<p>You can find these dependencies in <a class=\"reference external\" href=\"https://pip.pypa.io/en/latest/user_guide.html#requirements-files\">pip requirements files</a> inside the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">tests/requirements</span></code> directory of the Django source tree and install them\nlike so:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>pip<span class=\"w\"> </span>install<span class=\"w\"> </span>-r<span class=\"w\"> </span>tests/requirements/py3.txt<span class=\"w\">  </span><span class=\"c1\"># Python 2: py2.txt</span>\n</code></pre></div>\n<p>You can also install the database adapter(s) of your choice using\n<code class=\"docutils literal notranslate\"><span class=\"pre\">oracle.txt</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">mysql.txt</span></code>, or <code class=\"docutils literal notranslate\"><span class=\"pre\">postgres.txt</span></code>.</p>\n<p>If you want to test the memcached cache backend, you’ll also need to define\na <a class=\"reference internal\" href=\"/es/1.9/ref/settings/#std-setting-CACHES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CACHES</span></code></a> setting that points at your memcached instance.</p>\n<p>To run the GeoDjango tests, you will need to <a class=\"reference internal\" href=\"/es/1.9/ref/contrib/gis/install/\"><span class=\"doc\">setup a spatial database\nand install the Geospatial libraries</span></a>.</p>\n<p>Each of these dependencies is optional. If you’re missing any of them, the\nassociated tests will be skipped.</p>\n</section>\n<section id=\"code-coverage\">\n<h3>Code coverage<a class=\"heading-anchor\" href=\"#code-coverage\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Contributors are encouraged to run coverage on the test suite to identify areas\nthat need additional tests. The coverage tool installation and use is described\nin <a class=\"reference internal\" href=\"/es/1.9/topics/testing/advanced/#topics-testing-code-coverage\"><span class=\"std std-ref\">testing code coverage</span></a>.</p>\n<p>Coverage should be run in a single process to obtain accurate statistics. To\nrun coverage on the Django test suite using the standard test settings:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>coverage<span class=\"w\"> </span>run<span class=\"w\"> </span>./runtests.py<span class=\"w\"> </span>--settings<span class=\"o\">=</span>test_sqlite<span class=\"w\"> </span>--parallel<span class=\"o\">=</span><span class=\"m\">1</span>\n</code></pre></div>\n<p>After running coverage, generate the html report by running:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>coverage<span class=\"w\"> </span>html\n</code></pre></div>\n<p>When running coverage for the Django tests, the included <code class=\"docutils literal notranslate\"><span class=\"pre\">.coveragerc</span></code>\nsettings file  defines <code class=\"docutils literal notranslate\"><span class=\"pre\">coverage_html</span></code> as the output directory for the report\nand also excludes several directories not relevant to the results\n(test code or external code included in Django).</p>\n</section>\n</section>\n<section id=\"contrib-apps\">\n<span id=\"id1\"></span><h2>Contrib apps<a class=\"heading-anchor\" href=\"#contrib-apps\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Tests for contrib apps can be found in the <code class=\"docutils literal notranslate\"><span class=\"pre\">tests/</span></code> directory, typically\nunder <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;app_name&gt;_tests</span></code>. For example, tests for <code class=\"docutils literal notranslate\"><span class=\"pre\">contrib.auth</span></code> are located\nin <code class=\"docutils literal notranslate\"><span class=\"pre\">tests/auth_tests</span></code>.</p>\n</section>\n<section id=\"troubleshooting\">\n<span id=\"troubleshooting-unit-tests\"></span><h2>Troubleshooting<a class=\"heading-anchor\" href=\"#troubleshooting\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"many-test-failures-with-unicodeencodeerror\">\n<h3>Many test failures with <code class=\"docutils literal notranslate\"><span class=\"pre\">UnicodeEncodeError</span></code><a class=\"heading-anchor\" href=\"#many-test-failures-with-unicodeencodeerror\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If the <code class=\"docutils literal notranslate\"><span class=\"pre\">locales</span></code> package is not installed, some tests will fail with a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">UnicodeEncodeError</span></code>.</p>\n<p>You can resolve this on Debian-based systems, for example, by running:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>apt-get<span class=\"w\"> </span>install<span class=\"w\"> </span>locales\n<span class=\"gp\">$ </span>dpkg-reconfigure<span class=\"w\"> </span>locales\n</code></pre></div>\n</section>\n<section id=\"tests-that-only-fail-in-combination\">\n<h3>Tests that only fail in combination<a class=\"heading-anchor\" href=\"#tests-that-only-fail-in-combination\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>In case a test passes when run in isolation but fails within the whole suite,\nwe have some tools to help analyze the problem.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">--bisect</span></code> option of <code class=\"docutils literal notranslate\"><span class=\"pre\">runtests.py</span></code> will run the failing test while\nhalving the test set it is run together with on each iteration, often making\nit possible to identify a small number of tests that may be related to the\nfailure.</p>\n<p>For example, suppose that the failing test that works on its own is\n<code class=\"docutils literal notranslate\"><span class=\"pre\">ModelTest.test_eq</span></code>, then using:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>--bisect<span class=\"w\"> </span>basic.tests.ModelTest.test_eq\n</code></pre></div>\n<p>will try to determine a test that interferes with the given one. First, the\ntest is run with the first half of the test suite. If a failure occurs, the\nfirst half of the test suite is split in two groups and each group is then run\nwith the specified test. If there is no failure with the first half of the test\nsuite, the second half of the test suite is run with the specified test and\nsplit appropriately as described earlier. The process repeats until the set of\nfailing tests is minimized.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">--pair</span></code> option runs the given test alongside every other test from the\nsuite, letting you check if another test has side-effects that cause the\nfailure. So:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>--pair<span class=\"w\"> </span>basic.tests.ModelTest.test_eq\n</code></pre></div>\n<p>will pair <code class=\"docutils literal notranslate\"><span class=\"pre\">test_eq</span></code> with every test label.</p>\n<p>With both <code class=\"docutils literal notranslate\"><span class=\"pre\">--bisect</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">--pair</span></code>, if you already suspect which cases\nmight be responsible for the failure, you may limit tests to be cross-analyzed\nby <a class=\"reference internal\" href=\"#runtests-specifying-labels\"><span class=\"std std-ref\">specifying further test labels</span></a> after\nthe first one:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>--pair<span class=\"w\"> </span>basic.tests.ModelTest.test_eq<span class=\"w\"> </span>queries<span class=\"w\"> </span>transactions\n</code></pre></div>\n<p>You can also try running any set of tests in reverse using the <code class=\"docutils literal notranslate\"><span class=\"pre\">--reverse</span></code>\noption in order to verify that executing tests in a different order does not\ncause any trouble:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>basic<span class=\"w\"> </span>--reverse\n</code></pre></div>\n</section>\n<section id=\"seeing-the-sql-queries-run-during-a-test\">\n<h3>Seeing the SQL queries run during a test<a class=\"heading-anchor\" href=\"#seeing-the-sql-queries-run-during-a-test\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you wish to examine the SQL being run in failing tests, you can turn on\n<a class=\"reference internal\" href=\"/es/1.9/topics/logging/#django-db-logger\"><span class=\"std std-ref\">SQL logging</span></a> using the <code class=\"docutils literal notranslate\"><span class=\"pre\">--debug-sql</span></code> option. If you\ncombine this with <code class=\"docutils literal notranslate\"><span class=\"pre\">--verbosity=2</span></code>, all SQL queries will be output:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>basic<span class=\"w\"> </span>--debug-sql\n</code></pre></div>\n<aside class=\"version-note version-added\" data-version=\"1.8\">\n<p class=\"version-note-title\">New in Django 1.8</p><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">--reverse</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">--debug-sql</span></code> options were added.</p>\n</aside>\n</section>\n<section id=\"seeing-the-full-traceback-of-a-test-failure\">\n<h3>Seeing the full traceback of a test failure<a class=\"heading-anchor\" href=\"#seeing-the-full-traceback-of-a-test-failure\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>By default tests are run in parallel with one process per core. When the tests\nare run in parallel, however, you’ll only see a truncated traceback for any\ntest failures. You can adjust this behavior with the <code class=\"docutils literal notranslate\"><span class=\"pre\">--parallel</span></code> option:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>./runtests.py<span class=\"w\"> </span>basic<span class=\"w\"> </span>--parallel<span class=\"o\">=</span><span class=\"m\">1</span>\n</code></pre></div>\n<p>You can also use the <code class=\"docutils literal notranslate\"><span class=\"pre\">DJANGO_TEST_PROCESSES</span></code> environment variable for this\npurpose.</p>\n<aside class=\"version-note version-added\" data-version=\"1.9\">\n<p class=\"version-note-title\">New in Django 1.9</p><p>Support for running tests in parallel and the <code class=\"docutils literal notranslate\"><span class=\"pre\">--parallel</span></code> option were\nadded.</p>\n</aside>\n</section>\n</section>","rootId":"unit-tests","toc":[{"title":"Running the unit tests","anchor":"running-the-unit-tests","children":[{"title":"Quickstart","anchor":"quickstart","children":[]},{"title":"Using another settings module","anchor":"using-another-settings-module","children":[]},{"title":"Running only some of the tests","anchor":"running-only-some-of-the-tests","children":[]},{"title":"Running the Selenium tests","anchor":"running-the-selenium-tests","children":[]},{"title":"Running all the tests","anchor":"running-all-the-tests","children":[]},{"title":"Code coverage","anchor":"code-coverage","children":[]}]},{"title":"Contrib apps","anchor":"contrib-apps","children":[]},{"title":"Troubleshooting","anchor":"troubleshooting","children":[{"title":"Many test failures with UnicodeEncodeError","anchor":"many-test-failures-with-unicodeencodeerror","children":[]},{"title":"Tests that only fail in combination","anchor":"tests-that-only-fail-in-combination","children":[]},{"title":"Seeing the SQL queries run during a test","anchor":"seeing-the-sql-queries-run-during-a-test","children":[]},{"title":"Seeing the full traceback of a test failure","anchor":"seeing-the-full-traceback-of-a-test-failure","children":[]}]}],"breadcrumbs":[{"docname":"internals/index","title":"Django internals","url":"/es/1.9/internals/"},{"docname":"internals/contributing/index","title":"Contributing to Django","url":"/es/1.9/internals/contributing/"},{"docname":"internals/contributing/writing-code/index","title":"Writing code","url":"/es/1.9/internals/contributing/writing-code/"}],"prev":{"docname":"internals/contributing/writing-code/coding-style","title":"Coding style","url":"/es/1.9/internals/contributing/writing-code/coding-style/"},"next":{"docname":"internals/contributing/writing-code/submitting-patches","title":"Submitting patches","url":"/es/1.9/internals/contributing/writing-code/submitting-patches/"},"formats":{"html":"/es/1.9/internals/contributing/writing-code/unit-tests/","markdown":"/es/1.9/internals/contributing/writing-code/unit-tests.md","json":"/es/1.9/internals/contributing/writing-code/unit-tests.json"},"source":"https://github.com/django/django/blob/stable/1.9.x/docs/internals/contributing/writing-code/unit-tests.txt","official":"https://docs.djangoproject.com/es/1.9/internals/contributing/writing-code/unit-tests/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","fr","ja","id","pt-br","es"]}