... トするために使います。また、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 ユニットテストクラスの階層 ... ... トするために使います。また、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 ... ... のモジュール、 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 ... ... ったユニットテストです: 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 ... ... olls アプリケーションの 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 savep ... 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_per ... ... olls アプリケーションの 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 savep ... 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_per ... 2026年10月08日