TestCaseとアサーションの記述
モデルとビューを自動的にテストします
「TestCaseとアサーションの記述」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDjango Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Django Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Why Write Tests?
Tests catch bugs before your users do. They let you change code with confidence, knowing a green run means nothing broke. ✅
Meet TestCase
Django gives you a TestCase base class. Subclass it and each method that starts with test_ becomes an automatic check.
from django.test import TestCaseYour First Test Class
Group related checks in one class. Here a TestCase subclass will hold all the tests for the Article model.
class ArticleTests(TestCase):
passA Fresh Database
TestCase runs each test in its own transaction and rolls it back afterward. Tests never pollute your real data or each other.
Writing a Test Method
Name every test method with a test_ prefix so Django's runner discovers and executes it for you.
def test_title_is_saved(self):
...Assert Equality
The classic check is assertEqual. It passes when two values match and fails loudly when they differ.
self.assertEqual(article.title, 'Hello')Assert True or False
For boolean conditions use assertTrue and assertFalse. They make intent crystal clear in your test.
self.assertTrue(article.is_published)Setting Up Shared Data
Need the same object in many tests? Build it once in setUp, which Django runs before every test method.
def setUp(self):
self.a = Article.objects.create(title='Hi')Testing a Model Method
Verify your model behaves as designed. Create an object, call its method, then assert the result is what you expect.
self.assertEqual(str(self.a), 'Hi')Run the Tests
Kick everything off with the test command. Django finds your test files, runs them, and reports passes and failures.
python manage.py testReading the Output
A dot means a pass, an F means a failure, and E means an error. The summary shows exactly which test broke and why.
Quick Check
Django needs to know which methods are tests.
Recap
You wrote a TestCase, used setUp for shared data, checked results with assertEqual and assertTrue, and ran it all with manage.py test. 🎉
よくある質問
「TestCaseとアサーションの記述」レッスンは無料ですか?
はい。「TestCaseとアサーションの記述」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。
「TestCaseとアサーションの記述」で何を学びますか?
モデルとビューを自動的にテストします ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Django Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDjango Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「TestCaseとアサーションの記述」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDjango Academyレッスンでコードを書いて実行できますか?
はい。すべてのDjango Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Paginatorクラス
- ListViewのページ操作
- TestCaseとアサーションの記述
- Test ClientとFixtures