... トの実行の仕方について説明します。 テストを書く Django のユニットテストには、Python スタンダードライブラリのモジュール、 unittest を使用します。このモジュールは、テストをクラスベースのアプローチで定義します。 次の例では、 unittest.TestCase のサブクラス ... と、テストユーティリティのデフォルトの動作として、ファイル名が test で始まるファイル内のすべてのテストケースクラス (つまり unittest.TestCase のサブクラス) を見つけ、それらのテストケースクラスから自動的にテストスイートを構築し、そのテストスイートを実行しま ... す。 unittest の詳細については、Python のドキュメントを読んでください。 どこにテストを書くべき? デフォルトの startapp テンプレートは、新 ... ... Contrib support for easy handling of static files . Django's testing framework now supports (and ships with a copy of) the unittest2 library . Wherever possible, new features are introduced in a backwards-compatible manner per our API stability policy po ... e served at STATIC_URL . See the reference documentation of the app for more details or learn how to manage static files . unittest2 のサポート Python 2.7 では unittest ライブラリに大きな変更が加えられ、非常に便利な機能が追加されました。すべての Django プ ... クトがこれらの新機能の恩恵を受けられるように、Django には Python 2.4 との互換性を保つためにバックポートされた Python 2.7 の unittest ライブラリのコピーである unittest2 が同梱されています。 To access this library, Django provides the django.utils.unittest ... ... 示的にアクティブな言語をセットする にあります。 カスタマイズ例 以下は、テストクライアントを使ったユニットテストです。 import unittest from django.test import Client class SimpleTest ( unittest . TestCase ): def setUp ( self ): # Every test needs a client. ... customers" ]), 5 ) 参考 django.test.RequestFactory 提供されるテストケースのクラス 標準の Python ユニットテストのクラスは、 unittest.TestCase のベースクラスを拡張しています。Django は、このベースクラスのいくつかの拡張を提供します。 Django ユニットテストク ... ラスの階層 通常の unittest.TestCase は、任意のサブクラスに変換できます。つまり、テストのベースクラスを unittest.TestCase からサブクラスに変更します。 ... ... れるので、その models.py ファイルにテスト専用のモデルを定義することもできます。 ほかのテストフレームワークを使う 明らかに、 unittest は唯一のPythonテストフレームワークではありません。Djangoは代替フレームワークに対する明示的なサポートを提供していませんが、 ... らの引数を使用できるようになります。 属性 DiscoverRunner. test_suite テストスイートのビルドに使用するクラス。デフォルトでは unittest.TestSuite に設定されています。テストを収集するために別のロジックを実装したい場合は、これをオーバーライドできます。 Discove ... ner これは低レベルテストランナーのクラスで、個々のテストを実行し、結果をフォーマットするために使用されます。デフォルトでは unittest.TextTestRunner に設定されています。似たような名前になっていますが、残念ながらこれは DiscoverRunner と同じ種類のクラスではあ ... ... に一般に、書いたコードが期待通りの動作をするかどうかを検証できます。 The preferred way to write tests in Django is using the unittest module built-in to the Python standard library. This is covered in detail in the テストを書いて実行する document. You can ... ... ckage in INSTALLED_APPS . The new runner ( django.test.runner.DiscoverRunner ) uses the test discovery features built into unittest2 (the version of unittest in the Python 2.7+ standard library, and bundled with Django). With test discovery, tests can be ... ult. This option is now ignored and can be removed. New test runner In order to maintain greater consistency with Python's unittest module, the new test runner ( django.test.runner.DiscoverRunner ) does not automatically support some types of tests that ... emoved in Django 1.8; doctest suites should be updated to work with the standard library's doctest module (or converted to unittest -compatible tests). If you wish to delay updates to your test suite, you can set your TEST_RUNNER setting to django.test.s ... ... tils.timezone , django.utils.timezone.get_default_timezone() and django.utils.timezone.get_fixed_timezone() . django.utils.unittest django.utils.unittest provided uniform access to the unittest2 library on all Python versions. Since unittest2 became the ... standard library's unittest module in Python 2.7, and Django 1.7 drops support for older Python versions, this module isn't useful anymore. It has bee ... n deprecated. Use unittest instead. django.utils.datastructures.SortedDict As OrderedDict was added to the standard library in Python 2.7, SortedDict ... ... ます。 django.test.utils.str_prefix() - Strings don't have 'u' prefixes in Python 3. django.test.utils.patch_logger() - Use unittest.TestCase.assertLogs() instead. django.utils.lru_cache.lru_cache() - Alias of functools.lru_cache() . django.utils.decorato ... verts ' to ' instead of the previous equivalent decimal code ' . The django-admin test -k option now works as the unittest -k option rather than as a shortcut for --keepdb . Support for pywatchman < 1.2.0 is removed. urlencode() now encodes iter ... ... tils.dictconfig will be removed. django.utils.importlib will be removed. django.utils.tzinfo will be removed. django.utils.unittest will be removed. The syncdb command will be removed. django.db.models.signals.pre_syncdb and django.db.models.signals.post ... ed. The templates attribute should be used instead. The django.test.simple.DjangoTestRunner will be removed. Instead use a unittest -native class. The features of the django.test.simple.DjangoTestRunner (including fail-fast and Ctrl-C test termination) c ... an be provided by unittest.TextTestRunner . The undocumented function django.contrib.formtools.utils.security_hash will be removed, instead use djang ... ... work so you can start transmuting fear into boredom (with apologies to Kent Beck). You can write tests based on doctest or unittest and test your views with a simple test client. There is also new support for "fixtures" -- initial data, stored in any of ... 2026年10月08日