... トするために使います。また、Django はこれらのフレームワーク向けの特別なサポートも提供しています。詳細については、 LiveServerTestCase のセクションを読んでください。 包括的なテストスイートを実現するには、これらすべてのタイプのテストを組み合わせて行うべきです ... テストクライアントを使ったユニットテストです。 import unittest from django.test import Client class SimpleTest ( unittest . TestCase ): def setUp ( self ): # Every test needs a client. self . client = Client () def test_details ( self ): # Issue a GET req ... " ]), 5 ) 参考 django.test.RequestFactory 提供されるテストケースのクラス 標準の Python ユニットテストのクラスは、 unittest.TestCase のベースクラスを拡張しています。Django は、このベースクラスのいくつかの拡張を提供します。 Django ユニットテストクラスの階層 ... ... のモジュール、 unittest を使用します。このモジュールは、テストをクラスベースのアプローチで定義します。 次の例では、 unittest.TestCase のサブクラスである django.test.TestCase から、テスト用の新しいサブクラスを作っています。各テストをトランザクションの内側で ... 実行することで、独立性を実現しています。 from django.test import TestCase from myapp.models import Animal class AnimalTestCase ( TestCase ): def setUp ( self ): Animal . objects . create ( name = ... トユーティリティのデフォルトの動作として、ファイル名が test で始まるファイル内のすべてのテストケースクラス (つまり unittest.TestCase のサブクラス) を見つけ、それらのテストケースクラスから自動的にテストスイートを構築し、そのテストスイートを実行します。 uni ... ... ったユニットテストです: from django.contrib.auth.models import AnonymousUser , User from django.test import RequestFactory , TestCase from .views import MyView , my_view class SimpleTest ( TestCase ): def setUp ( self ): # Every test needs access to the re ... トするには、まずビューをインスタンス化して setup() に request を渡します: tests.py from django.test import RequestFactory , TestCase from .views import HomeView class HomePageTest ( TestCase ): def test_environment_set_in_context ( self ): request = Reque ... ファイルに追加することです。例えば、docs.djangoproject.com のテストスイートには以下が含まれています: from django.test import TestCase class SearchFormTestCase ( TestCase ): def test_empty_get ( self ): response = self . client . get ( "/en/dev/search/" , h ... ... polls アプリケーションの tests.py ファイルに次のコードを書きます。 polls/tests.py import datetime from django.test import TestCase from django.utils import timezone from .models import Question class QuestionModelTests ( TestCase ): def test_was_publish ... ed_recently (), False ) ここでは、未来の日付の pub_date を持つ Question のインスタンスを生成するメソッドを持つ django.test.TestCase を継承したサブクラスを作っています。それから、 was_published_recently() の出力をチェックしています。これは False になるはず ... こでは以下のようなことが起こりました。 manage.py test polls は、 polls アプリケーション内にあるテストを探します django.test.TestCase クラスのサブクラスを発見します テストのための特別なデータベースを作成します テスト用のメソッドとして、 test で始まるメソッ ... ... n queries. A collection of database functions is also included with functionality such as Coalesce , Concat , and Substr . TestCase data setup TestCase has been refactored to allow for data initialization at the class level using transactions and savepoi ... ents. Tests are now run within two nested atomic() blocks: one for the whole class and one for each test. The class method TestCase.setUpTestData() adds the ability to set up test data at the class level. Using this technique can speed up the tests as co ... mpared to using setUp() . Fixture loading within TestCase is now performed once for the whole TestCase . マイナーな機能 django.contrib.admin ModelAdmin now has a has_module_permiss ... ... ブポイントを使い、特に delete() と update() クエリにおいて、複数のクエリを要求する ORM 操作の信頼性を担保します。 Django の TestCase クラスは、パフォーマンス向上のため、各テストをトランザクションでラップします。 HTTP リクエストにトランザクションを結びつけ ... エラーはすべて捕捉され、 django.db.backends.base ロガーに記録されます。 on_commit() で登録されたコールバックをテストするには TestCase.captureOnCommitCallbacks() を使用します。 セーブポイント セーブポイント(ネストされた atomic() ブロック)は正しく処理されま ... 。オートコミットが無効でアトミックブロック内にいない時に on_commit() を呼び出すとエラーになります。 テストでの使用 Django の TestCase クラスは各テストをトランザクションでラップし、テストの分離を行うために、各テストの後にそのトランザクションをロールバックし ... ... not raised when using the db_column name of a ForeignKey field with primary_key=True ( #12768 ). Prevented an exception in TestCase.setUpTestData() from leaking the transaction ( #25176 ). Fixed has_changed() method in contrib.postgres.forms.HStoreField ... ... =True to serializers.deserialize() . Additionally, loaddata handles forward references automatically. テスト The new SimpleTestCase.assertURLEqual() assertion checks for a given URL, ignoring the ordering of the query string. assertRedirects() uses the n ... ng allows using Oracle Managed Files (OMF) tablespaces. Deferrable database constraints are now checked at the end of each TestCase test on SQLite 3.20+, just like on other backends that support deferrable constraints. These checks aren't implemented for ... seAdmin ): actions = BaseAdmin . actions + [ "b" ] django.contrib.gis Support for GDAL 1.9 and 1.10 is dropped. TransactionTestCase serialized data loading Initial data migrations are now loaded in TransactionTestCase at the end of the test, after the da ... ... hed with Cached template loaders are now correctly reloaded in development. テスト Objects assigned to class attributes in TestCase.setUpTestData() are now isolated for each test method. Such objects are now required to support creating deep copies with ... sing the test --timing option. Client now preserves the request query string when following 307 and 308 redirects. The new TestCase.captureOnCommitCallbacks() method captures callback functions passed to transaction.on_commit() in a list. This allows you ... to test such callbacks without using the slower TransactionTestCase . TransactionTestCase.assertQuerysetEqual() now supports direct comparison against another queryset rather than being rest ... ... in inputs to multipart forms could result in an infinite loop when parsing files. バグ修正 Fixed a bug in Django 4.0 where TestCase.captureOnCommitCallbacks() could execute callbacks multiple times ( #33410 ). Fixed a regression in Django 4.0 where help_ ... 2026年10月08日