{"title":"Les tests unitaires","version":"2.1","locale":"fr","docname":"internals/contributing/writing-code/unit-tests","url":"/fr/2.1/internals/contributing/writing-code/unit-tests/","canonical":"https://djangodocs.dev/fr/2.1/internals/contributing/writing-code/unit-tests/","summary":"Django est livré avec sa propre suite de tests, dans le répertoire tests de la base de code.","html":"<h1>Les tests unitaires<a class=\"heading-anchor\" href=\"#unit-tests\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Django est livré avec sa propre suite de tests, dans le répertoire <code class=\"docutils literal notranslate\">tests</code> de la base de code. Notre politique est de nous assurer que tous les tests passent en tout temps.</p>\n<p>Nous apprécions toute contribution à la suite de tests !</p>\n<p>Les tests de Django utilisent tous l’infrastructure de tests livrée avec Django pour tester les applications. Consultez <a class=\"reference internal\" href=\"/fr/2.1/topics/testing/overview/\"><span class=\"doc\">Écriture et lancement de tests</span></a> pour une explication sur la façon d’écrire de nouveaux tests.</p>\n<section id=\"running-the-unit-tests\">\n<span id=\"running-unit-tests\"></span><h2>Lancement des tests unitaires<a class=\"heading-anchor\" href=\"#running-the-unit-tests\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"quickstart\">\n<h3>Démarrage rapide<a class=\"heading-anchor\" href=\"#quickstart\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Tout d’abord, créez votre <a class=\"reference external\" href=\"https://github.com/django/django/fork\">fork de Django sur GitHub</a>.</p>\n<p>Ensuite, créez et activez un environnement virtuel. Si vous n’êtes pas familier avec cette procédure, lisez le <a class=\"reference internal\" href=\"/fr/2.1/intro/contributing/\"><span class=\"doc\">tutoriel de contribution</span></a>.</p>\n<p>Puis, créez un clone local de votre fork, installez quelques dépendances et lancez la suite de 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 clone git@github.com:YourGitHubName/django.git django-repo\n<span class=\"gp\">$ </span><span class=\"nb\">cd</span> django-repo/tests\n<span class=\"gp\">$ </span>pip install -e ..\n<span class=\"gp\">$ </span>pip install -r requirements/py3.txt\n<span class=\"gp\">$ </span>./runtests.py\n</code></pre></div>\n<p>L’installation des dépendances nécessite probablement l’installation de paquets de votre système d’exploitation. Il est généralement possible de savoir quels paquets installer en faisant une recherche Web à propos des dernières lignes du message d’erreur. Essayez d’ajouter le nom de votre système d’exploitation à la requête si nécessaire.</p>\n<p>Si vous avez des soucis d’installation des dépendances, vous pouvez omettre cette étape. Voir <a class=\"reference internal\" href=\"#running-unit-tests-dependencies\"><span class=\"std std-ref\">Running all the tests</span></a> pour des détails sur l’installation des dépendances facultatives des tests. Si l’une des dépendances facultatives n’est pas installée, les tests qui l’exigent seront passés (skipped).</p>\n<p>Le lancement des tests exige un module de réglages Django définissant la base de données à utiliser. Pour faciliter le démarrage, Django fournit et utilise un module de réglages d’exemple utilisant la base de données SQLite. Voir <a class=\"reference internal\" href=\"#running-unit-tests-settings\"><span class=\"std std-ref\">Utilisation d’un autre module de réglages settings</span></a> pour apprendre comment utiliser un module de réglages différent pour lancer les tests avec une autre base de données.</p>\n<aside class=\"admonition-windows-users admonition\" role=\"note\">\n<p class=\"admonition-title\">Utilisateurs de Windows</p>\n<p>Nous recommandons quelque chose comme <a class=\"reference external\" href=\"https://msysgit.github.io/\">Git Bash</a> pour lancer les tests en utilisant l’approche ci-dessus.</p>\n</aside>\n<p>Des problèmes ? Voir <a class=\"reference internal\" href=\"#troubleshooting-unit-tests\"><span class=\"std std-ref\">Dépannage</span></a> pour certaines problématiques courantes.</p>\n</section>\n<section id=\"running-tests-using-tox\">\n<h3>Lancement des tests avec <code class=\"docutils literal notranslate\">tox</code><a class=\"heading-anchor\" href=\"#running-tests-using-tox\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><a class=\"reference external\" href=\"https://tox.readthedocs.io/\">Tox</a> is a tool for running tests in different\nvirtual environments. Django includes a basic <code class=\"docutils literal notranslate\">tox.ini</code> that automates some\nchecks that our build server performs on pull requests. To run the unit tests\nand other checks (such as <a class=\"reference internal\" href=\"/fr/2.1/internals/contributing/writing-code/coding-style/#coding-style-imports\"><span class=\"std std-ref\">import sorting</span></a>, the\n<a class=\"reference internal\" href=\"/fr/2.1/internals/contributing/writing-documentation/#documentation-spelling-check\"><span class=\"std std-ref\">documentation spelling checker</span></a>, and\n<a class=\"reference internal\" href=\"/fr/2.1/internals/contributing/writing-code/coding-style/#coding-style-python\"><span class=\"std std-ref\">code formatting</span></a>), install and run the <code class=\"docutils literal notranslate\">tox</code>\ncommand from any place in the Django source tree:</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 install tox\n<span class=\"gp\">$ </span>tox\n</code></pre></div>\n<p>By default, <code class=\"docutils literal notranslate\">tox</code> runs the test suite with the bundled test settings file for\nSQLite, <code class=\"docutils literal notranslate\">flake8</code>, <code class=\"docutils literal notranslate\">isort</code>, and the documentation spelling checker. In\naddition to the system dependencies noted elsewhere in this documentation,\nthe command <code class=\"docutils literal notranslate\">python3</code> must be on your path and linked to the appropriate\nversion of Python. A list of default environments can be seen as follows:</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>tox -l\n<span class=\"go\">py3</span>\n<span class=\"go\">flake8</span>\n<span class=\"go\">docs</span>\n<span class=\"go\">isort</span>\n</code></pre></div>\n<section id=\"testing-other-python-versions-and-database-backends\">\n<h4>Testing other Python versions and database backends<a class=\"heading-anchor\" href=\"#testing-other-python-versions-and-database-backends\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>In addition to the default environments, <code class=\"docutils literal notranslate\">tox</code> supports running unit tests\nfor other versions of Python and other database backends. Since Django’s test\nsuite doesn’t bundle a settings file for database backends other than SQLite,\nhowever, you must <a class=\"reference internal\" href=\"#running-unit-tests-settings\"><span class=\"std std-ref\">create and provide your own test settings</span></a>. For example, to run the tests on Python 3.5\nusing PostgreSQL:</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>tox -e py35-postgres -- --settings<span class=\"o\">=</span>my_postgres_settings\n</code></pre></div>\n<p>This command sets up a Python 3.5 virtual environment, installs Django’s\ntest suite dependencies (including those for PostgreSQL), and calls\n<code class=\"docutils literal notranslate\">runtests.py</code> with the supplied arguments (in this case,\n<code class=\"docutils literal notranslate\">--settings=my_postgres_settings</code>).</p>\n<p>The remainder of this documentation shows commands for running tests without\n<code class=\"docutils literal notranslate\">tox</code>, however, any option passed to <code class=\"docutils literal notranslate\">runtests.py</code> can also be passed to\n<code class=\"docutils literal notranslate\">tox</code> by prefixing the argument list with <code class=\"docutils literal notranslate\">--</code>, as above.</p>\n<p>Tox also respects the <code class=\"docutils literal notranslate\">DJANGO_SETTINGS_MODULE</code> environment variable, if set.\nFor example, the following is equivalent to the command above:</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><span class=\"nv\">DJANGO_SETTINGS_MODULE</span><span class=\"o\">=</span>my_postgres_settings tox -e py35-postgres\n</code></pre></div>\n</section>\n<section id=\"running-the-javascript-tests\">\n<h4>Running the JavaScript tests<a class=\"heading-anchor\" href=\"#running-the-javascript-tests\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Django includes a set of <a class=\"reference internal\" href=\"/fr/2.1/internals/contributing/writing-code/javascript/#javascript-tests\"><span class=\"std std-ref\">JavaScript unit tests</span></a> for\nfunctions in certain contrib apps. The JavaScript tests aren’t run by default\nusing <code class=\"docutils literal notranslate\">tox</code> because they require Node.js to be installed and aren’t\nnecessary for the majority of patches. To run the JavaScript tests using\n<code class=\"docutils literal notranslate\">tox</code>:</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>tox -e javascript\n</code></pre></div>\n<p>This command runs <code class=\"docutils literal notranslate\">npm install</code> to ensure test requirements are up to\ndate and then runs <code class=\"docutils literal notranslate\">npm test</code>.</p>\n</section>\n</section>\n<section id=\"using-another-settings-module\">\n<span id=\"running-unit-tests-settings\"></span><h3>Utilisation d’un autre module de réglages <code class=\"docutils literal notranslate\">settings</code><a class=\"heading-anchor\" href=\"#using-another-settings-module\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Le module de réglages inclus (<code class=\"docutils literal notranslate\">tests/test_sqlite.py</code>) permet de lancer la suite de tests avec SQLite. Si vous souhaitez lancer les tests avec une autre base de données, vous devez définir votre propre fichier de réglages. Certains tests, comme ceux de <code class=\"docutils literal notranslate\">contrib.postgres</code>, sont spécifique à un moteur de base de données spécifique et seront omis quand une autre base de données est utilisée.</p>\n<p>Pour lancer les tests avec des réglages différents, assurez-vous que le module se trouve dans le chemin <code class=\"docutils literal notranslate\">PYTHONPATH</code> et indiquez ce module avec <code class=\"docutils literal notranslate\">--settings</code>.</p>\n<p>Le réglage <a class=\"reference internal\" href=\"/fr/2.1/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\">DATABASES</code></a> dans tout module de réglages pour les tests doit définir deux bases de données :</p>\n<ul class=\"simple\">\n<li><p>Une base de données <code class=\"docutils literal notranslate\">default</code>. Celle-ci doit utiliser le moteur que vous souhaitez tester de manière principale.</p></li>\n<li><p>Une base de données avec l’alias <code class=\"docutils literal notranslate\">other</code>. Celle-ci est utilisée pour tester les requêtes qui sont dirigées vers d’autres bases de données. Elle devrait utiliser le même moteur que la base de données <code class=\"docutils literal notranslate\">default</code>, mais elle doit avoir un autre nom.</p></li>\n</ul>\n<p>Si vous utilisez un moteur autre que SQLite, il est nécessaire de fournir d’autres détails pour chaque base de données :</p>\n<ul class=\"simple\">\n<li><p>L’option <a class=\"reference internal\" href=\"/fr/2.1/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\">USER</code></a> doit indiquer un compte utilisateur existant pour la base de données. Cet utilisateur a besoin de la permission d’exécuter <code class=\"docutils literal notranslate\">CREATE DATABASE</code> afin de pouvoir créer la base de données de test.</p></li>\n<li><p>L’option <a class=\"reference internal\" href=\"/fr/2.1/ref/settings/#std-setting-PASSWORD\"><code class=\"xref std std-setting docutils literal notranslate\">PASSWORD</code></a> doit indiquer le mot de passe à utiliser pour l’utilisateur <a class=\"reference internal\" href=\"/fr/2.1/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\">USER</code></a>.</p></li>\n</ul>\n<p>Test databases get their names by prepending <code class=\"docutils literal notranslate\">test_</code> to the value of the\n<a class=\"reference internal\" href=\"/fr/2.1/ref/settings/#std-setting-NAME\"><code class=\"xref std std-setting docutils literal notranslate\">NAME</code></a> settings for the databases defined in <a class=\"reference internal\" href=\"/fr/2.1/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\">DATABASES</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=\"/fr/2.1/ref/settings/#std-setting-TEST_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\">CHARSET</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\">Lien vers cette rubrique</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\">runtests.py</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 --settings<span class=\"o\">=</span>path.to.settings generic_relations i18n\n</code></pre></div>\n<p>How do you find out the names of individual tests? Look in <code class=\"docutils literal notranslate\">tests/</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\">TranslationTests</code>\nof the <code class=\"docutils literal notranslate\">i18n</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 --settings<span class=\"o\">=</span>path.to.settings 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 --settings<span class=\"o\">=</span>path.to.settings 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\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Some tests require Selenium and a Web browser. To run these tests, you must\ninstall the <a class=\"reference external\" href=\"https://pypi.org/project/selenium/\">selenium</a> package and run the tests with the\n<code class=\"docutils literal notranslate\">--selenium=&lt;BROWSERS&gt;</code> option. For example, if you have Firefox and Google\nChrome installed:</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 --selenium<span class=\"o\">=</span>firefox,chrome\n</code></pre></div>\n<p>See the <a class=\"reference external\" href=\"https://github.com/SeleniumHQ/selenium/tree/master/py/selenium/webdriver\">selenium.webdriver</a> package for the list of available browsers.</p>\n<p>Specifying <code class=\"docutils literal notranslate\">--selenium</code> automatically sets <code class=\"docutils literal notranslate\">--tags=selenium</code> to run only\nthe tests that require selenium.</p>\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\">Lien vers cette rubrique</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.org/project/argon2_cffi/\">argon2-cffi</a> 16.1.0+</p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/bcrypt/\">bcrypt</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/docutils/\">docutils</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/geoip2/\">geoip2</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/jinja2/\">jinja2</a> 2.7+</p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/numpy/\">numpy</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/Pillow/\">Pillow</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pyyaml.org/wiki/PyYAML\">PyYAML</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/pytz/\">pytz</a> (required)</p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/setuptools/\">setuptools</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://memcached.org/\">memcached</a>, plus a <a class=\"reference internal\" href=\"/fr/2.1/topics/cache/#memcached\"><span class=\"std std-ref\">supported Python binding</span></a></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=\"/fr/2.1/topics/i18n/translation/#gettext-on-windows\"><span class=\"std std-ref\">gettext et Windows</span></a>)</p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/selenium/\">selenium</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://pypi.org/project/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/#requirements-files\">pip requirements files</a> inside the\n<code class=\"docutils literal notranslate\">tests/requirements</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 install -r tests/requirements/py3.txt\n</code></pre></div>\n<p>Si vous rencontrez une erreur durant l’installation, il est possible qu’une dépendance pour un ou plusieurs paquets Python manque sur votre système. Consultez la documentation du paquet problématique ou recherchez sur le Web avec le message d’erreur obtenu.</p>\n<p>You can also install the database adapter(s) of your choice using\n<code class=\"docutils literal notranslate\">oracle.txt</code>, <code class=\"docutils literal notranslate\">mysql.txt</code>, or <code class=\"docutils literal notranslate\">postgres.txt</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=\"/fr/2.1/ref/settings/#std-setting-CACHES\"><code class=\"xref std std-setting docutils literal notranslate\">CACHES</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=\"/fr/2.1/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>La couverture de code<a class=\"heading-anchor\" href=\"#code-coverage\"><span class=\"visually-hidden\">Lien vers cette rubrique</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=\"/fr/2.1/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 run ./runtests.py --settings<span class=\"o\">=</span>test_sqlite --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 html\n</code></pre></div>\n<p>When running coverage for the Django tests, the included <code class=\"docutils literal notranslate\">.coveragerc</code>\nsettings file  defines <code class=\"docutils literal notranslate\">coverage_html</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\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Les tests des applications contribuées se trouvent dans le répertoire <code class=\"docutils literal notranslate\">tests/</code>, typiquement sous <code class=\"docutils literal notranslate\">&lt;nom_app&gt;_tests</code>. Par exemple, les tests de <code class=\"docutils literal notranslate\">contrib.auth</code> se trouvent dans <code class=\"docutils literal notranslate\">tests/auth_tests</code>.</p>\n</section>\n<section id=\"troubleshooting\">\n<span id=\"troubleshooting-unit-tests\"></span><h2>Dépannage<a class=\"heading-anchor\" href=\"#troubleshooting\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"many-test-failures-with-unicodeencodeerror\">\n<h3>Nombreux échecs de tests avec <code class=\"docutils literal notranslate\">UnicodeEncodeError</code><a class=\"heading-anchor\" href=\"#many-test-failures-with-unicodeencodeerror\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Si le paquet <code class=\"docutils literal notranslate\">locales</code> n’est pas installé, certains tests échoueront avec une exception <code class=\"docutils literal notranslate\">UnicodeEncodeError</code>.</p>\n<p>Il est possible de corriger cela par exemple sur les systèmes basés sur Debian en lançant</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 install locales\n<span class=\"gp\">$ </span>dpkg-reconfigure locales\n</code></pre></div>\n<p>Il est possible de corriger cela sur les systèmes macOS en configurant la locale du shell</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><span class=\"nb\">export</span> <span class=\"nv\">LANG</span><span class=\"o\">=</span><span class=\"s2\">&quot;en_US.UTF-8&quot;</span>\n<span class=\"gp\">$ </span><span class=\"nb\">export</span> <span class=\"nv\">LC_ALL</span><span class=\"o\">=</span><span class=\"s2\">&quot;en_US.UTF-8&quot;</span>\n</code></pre></div>\n<p>Lancez la commande <code class=\"docutils literal notranslate\">locale</code> pour confirmer la modification. Une autre possibilité est d’ajouter ces commandes d’exportation au fichier de démarrage du shell (par ex. <code class=\"docutils literal notranslate\">~/.bashrc</code> pour Bash) afin d’éviter de devoir les retaper.</p>\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\">Lien vers cette rubrique</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\">--bisect</code> option of <code class=\"docutils literal notranslate\">runtests.py</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\">ModelTest.test_eq</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 --bisect 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\">--pair</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 --pair basic.tests.ModelTest.test_eq\n</code></pre></div>\n<p>will pair <code class=\"docutils literal notranslate\">test_eq</code> with every test label.</p>\n<p>With both <code class=\"docutils literal notranslate\">--bisect</code> and <code class=\"docutils literal notranslate\">--pair</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 --pair basic.tests.ModelTest.test_eq queries 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\">--reverse</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 basic --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\">Lien vers cette rubrique</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=\"/fr/2.1/topics/logging/#django-db-logger\"><span class=\"std std-ref\">SQL logging</span></a> using the <code class=\"docutils literal notranslate\">--debug-sql</code> option. If you\ncombine this with <code class=\"docutils literal notranslate\">--verbosity=2</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 basic --debug-sql\n</code></pre></div>\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\">Lien vers cette rubrique</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\">--parallel</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 basic --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\">DJANGO_TEST_PROCESSES</code> environment variable for this\npurpose.</p>\n</section>\n<section id=\"tips-for-writing-tests\">\n<h3>Tips for writing tests<a class=\"heading-anchor\" href=\"#tips-for-writing-tests\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h3>\n<section id=\"isolating-model-registration\">\n<h4>Isolating model registration<a class=\"heading-anchor\" href=\"#isolating-model-registration\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>To avoid polluting the global <a class=\"reference internal\" href=\"/fr/2.1/ref/applications/#django.apps.apps\" title=\"django.apps.apps\"><code class=\"xref py py-attr docutils literal notranslate\">apps</code></a> registry and prevent\nunnecessary table creation, models defined in a test method should be bound to\na temporary <code class=\"docutils literal notranslate\">Apps</code> instance:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.apps.registry</span> <span class=\"kn\">import</span> Apps\n<span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n<span class=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> SimpleTestCase\n\n<span class=\"k\">class</span> <span class=\"nc\">TestModelDefinition</span><span class=\"p\">(</span>SimpleTestCase<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_model_definition</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        test_apps <span class=\"o\">=</span> Apps<span class=\"p\">([</span><span class=\"s1\">&#39;app_label&#39;</span><span class=\"p\">])</span>\n\n        <span class=\"k\">class</span> <span class=\"nc\">TestModel</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n            <span class=\"k\">class</span> <span class=\"nc\">Meta</span><span class=\"p\">:</span>\n                apps <span class=\"o\">=</span> test_apps\n        <span class=\"o\">...</span>\n</code></pre></div>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.test.utils.isolate_apps\">\n<span class=\"sig-prename descclassname\">django.test.utils.</span><span class=\"sig-name descname\">isolate_apps</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">*</span>app_labels</em>, <em class=\"sig-param\">attr_name<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">kwarg_name<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.test.utils.isolate_apps\"><span class=\"visually-hidden\">Lien vers cette définition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Since this pattern involves a lot of boilerplate, Django provides the\n<a class=\"reference internal\" href=\"#django.test.utils.isolate_apps\" title=\"django.test.utils.isolate_apps\"><code class=\"xref py py-func docutils literal notranslate\">isolate_apps()</code></a> decorator. It’s used like this:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n<span class=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> SimpleTestCase\n<span class=\"kn\">from</span> <span class=\"nn\">django.test.utils</span> <span class=\"kn\">import</span> isolate_apps\n\n<span class=\"k\">class</span> <span class=\"nc\">TestModelDefinition</span><span class=\"p\">(</span>SimpleTestCase<span class=\"p\">):</span>\n    <span class=\"nd\">@isolate_apps</span><span class=\"p\">(</span><span class=\"s1\">&#39;app_label&#39;</span><span class=\"p\">)</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_model_definition</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">class</span> <span class=\"nc\">TestModel</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n            <span class=\"k\">pass</span>\n        <span class=\"o\">...</span>\n</code></pre></div>\n<aside class=\"admonition-setting-app-label admonition\" role=\"note\">\n<p class=\"admonition-title\">Setting <code class=\"docutils literal notranslate\">app_label</code></p>\n<p>Models defined in a test method with no explicit\n<a class=\"reference internal\" href=\"/fr/2.1/ref/models/options/#django.db.models.Options.app_label\" title=\"django.db.models.Options.app_label\"><code class=\"xref py py-attr docutils literal notranslate\">app_label</code></a> are automatically assigned the\nlabel of the app in which their test class is located.</p>\n<p>In order to make sure the models defined within the context of\n<a class=\"reference internal\" href=\"#django.test.utils.isolate_apps\" title=\"django.test.utils.isolate_apps\"><code class=\"xref py py-func docutils literal notranslate\">isolate_apps()</code></a> instances are correctly\ninstalled, you should pass the set of targeted <code class=\"docutils literal notranslate\">app_label</code> as arguments:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\">tests/app_label/tests.py</figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n<span class=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> SimpleTestCase\n<span class=\"kn\">from</span> <span class=\"nn\">django.test.utils</span> <span class=\"kn\">import</span> isolate_apps\n\n<span class=\"k\">class</span> <span class=\"nc\">TestModelDefinition</span><span class=\"p\">(</span>SimpleTestCase<span class=\"p\">):</span>\n    <span class=\"nd\">@isolate_apps</span><span class=\"p\">(</span><span class=\"s1\">&#39;app_label&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;other_app_label&#39;</span><span class=\"p\">)</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_model_definition</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"c1\"># This model automatically receives app_label=&#39;app_label&#39;</span>\n        <span class=\"k\">class</span> <span class=\"nc\">TestModel</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n            <span class=\"k\">pass</span>\n\n        <span class=\"k\">class</span> <span class=\"nc\">OtherAppModel</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n            <span class=\"k\">class</span> <span class=\"nc\">Meta</span><span class=\"p\">:</span>\n                app_label <span class=\"o\">=</span> <span class=\"s1\">&#39;other_app_label&#39;</span>\n        <span class=\"o\">...</span>\n</code></pre></figure>\n</aside>\n<p>The decorator can also be applied to classes:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n<span class=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> SimpleTestCase\n<span class=\"kn\">from</span> <span class=\"nn\">django.test.utils</span> <span class=\"kn\">import</span> isolate_apps\n\n<span class=\"nd\">@isolate_apps</span><span class=\"p\">(</span><span class=\"s1\">&#39;app_label&#39;</span><span class=\"p\">)</span>\n<span class=\"k\">class</span> <span class=\"nc\">TestModelDefinition</span><span class=\"p\">(</span>SimpleTestCase<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_model_definition</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">class</span> <span class=\"nc\">TestModel</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n            <span class=\"k\">pass</span>\n        <span class=\"o\">...</span>\n</code></pre></div>\n<p>The temporary <code class=\"docutils literal notranslate\">Apps</code> instance used to isolate model registration can be\nretrieved as an attribute when used as a class decorator by using the\n<code class=\"docutils literal notranslate\">attr_name</code> parameter:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n<span class=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> SimpleTestCase\n<span class=\"kn\">from</span> <span class=\"nn\">django.test.utils</span> <span class=\"kn\">import</span> isolate_apps\n\n<span class=\"nd\">@isolate_apps</span><span class=\"p\">(</span><span class=\"s1\">&#39;app_label&#39;</span><span class=\"p\">,</span> attr_name<span class=\"o\">=</span><span class=\"s1\">&#39;apps&#39;</span><span class=\"p\">)</span>\n<span class=\"k\">class</span> <span class=\"nc\">TestModelDefinition</span><span class=\"p\">(</span>SimpleTestCase<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_model_definition</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">class</span> <span class=\"nc\">TestModel</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n            <span class=\"k\">pass</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertIs<span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span>apps<span class=\"o\">.</span>get_model<span class=\"p\">(</span><span class=\"s1\">&#39;app_label&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;TestModel&#39;</span><span class=\"p\">),</span> TestModel<span class=\"p\">)</span>\n</code></pre></div>\n<p>Or as an argument on the test method when used as a method decorator by using\nthe <code class=\"docutils literal notranslate\">kwarg_name</code> parameter:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n<span class=\"kn\">from</span> <span class=\"nn\">django.test</span> <span class=\"kn\">import</span> SimpleTestCase\n<span class=\"kn\">from</span> <span class=\"nn\">django.test.utils</span> <span class=\"kn\">import</span> isolate_apps\n\n<span class=\"k\">class</span> <span class=\"nc\">TestModelDefinition</span><span class=\"p\">(</span>SimpleTestCase<span class=\"p\">):</span>\n    <span class=\"nd\">@isolate_apps</span><span class=\"p\">(</span><span class=\"s1\">&#39;app_label&#39;</span><span class=\"p\">,</span> kwarg_name<span class=\"o\">=</span><span class=\"s1\">&#39;apps&#39;</span><span class=\"p\">)</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_model_definition</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> apps<span class=\"p\">):</span>\n        <span class=\"k\">class</span> <span class=\"nc\">TestModel</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n            <span class=\"k\">pass</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertIs<span class=\"p\">(</span>apps<span class=\"o\">.</span>get_model<span class=\"p\">(</span><span class=\"s1\">&#39;app_label&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;TestModel&#39;</span><span class=\"p\">),</span> TestModel<span class=\"p\">)</span>\n</code></pre></div>\n</section>\n</section>\n</section>","rootId":"unit-tests","toc":[{"title":"Lancement des tests unitaires","anchor":"running-the-unit-tests","children":[{"title":"Démarrage rapide","anchor":"quickstart","children":[]},{"title":"Lancement des tests avec tox","anchor":"running-tests-using-tox","children":[{"title":"Testing other Python versions and database backends","anchor":"testing-other-python-versions-and-database-backends","children":[]},{"title":"Running the JavaScript tests","anchor":"running-the-javascript-tests","children":[]}]},{"title":"Utilisation d’un autre module de réglages settings","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":"La couverture de code","anchor":"code-coverage","children":[]}]},{"title":"Contrib apps","anchor":"contrib-apps","children":[]},{"title":"Dépannage","anchor":"troubleshooting","children":[{"title":"Nombreux échecs de tests avec 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":[]},{"title":"Tips for writing tests","anchor":"tips-for-writing-tests","children":[{"title":"Isolating model registration","anchor":"isolating-model-registration","children":[]}]}]}],"breadcrumbs":[{"docname":"internals/index","title":"Fonctionnement interne du projet Django","url":"/fr/2.1/internals/"},{"docname":"internals/contributing/index","title":"Contribuer à Django","url":"/fr/2.1/internals/contributing/"},{"docname":"internals/contributing/writing-code/index","title":"Écriture du code","url":"/fr/2.1/internals/contributing/writing-code/"}],"prev":{"docname":"internals/contributing/writing-code/coding-style","title":"Coding style","url":"/fr/2.1/internals/contributing/writing-code/coding-style/"},"next":{"docname":"internals/contributing/writing-code/submitting-patches","title":"Soumission de correctifs","url":"/fr/2.1/internals/contributing/writing-code/submitting-patches/"},"formats":{"html":"/fr/2.1/internals/contributing/writing-code/unit-tests/","markdown":"/fr/2.1/internals/contributing/writing-code/unit-tests.md","json":"/fr/2.1/internals/contributing/writing-code/unit-tests.json"},"source":"https://github.com/django/django/blob/stable/2.1.x/docs/internals/contributing/writing-code/unit-tests.txt","official":"https://docs.djangoproject.com/fr/2.1/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","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}